Wednesday, April 11, 2012

wrong prefix table when joins option is used in unit test

Hi there,

I'm writing several unit tests in my cakephp models. Yesterday, I
found something disgusting when I was testing the find method. The
problem basically is that the system is not able to add the
table_prefix to the given table when you use the 'table' option in
joins conditions. An example of that is as follows:

I have Perception and PerceptionDetail models...and I want to select
them by using joins.

$this->find('first', array(
'conditions' => array(
'Perception.vatin' => $vatin,
),
'joins' => array(
array(
'table' => 'perception_details',
'alias' => 'PerceptionDetail',
'conditions' => array(
'PerceptionDetail.perception_id = Perception.id'
)
)
)
)
);

The system builds the following sql query:

SELECT
`Perception`.`id`, `Perception`.`vatin`,
`Perception`.`jurisdiction`, `Perception`.`tax_regime_type`
FROM
`test_suite_perceptions` AS `Perception`
JOIN perception_details AS `PerceptionDetail`
ON (`PerceptionDetail`.`perception_id` .= `Perception`.`id`)
WHERE `Perception`.`vatin` = '3071104396500' LIMIT 1

As you can see, perception is translated to test_suite_perceptions
*but* perception_details remains equal :(

A workaround that I found is to use the contain option as follows:

$this->find('first', array(
'conditions'=> array(
'Perception.vatin' => $vatin,
'Perception.jurisdiction' => $jurisdiction,
),
'contain' => array(
'PerceptionDetail' => array(
'conditions' => array(
..... #my_conditions
),
)
),
'cacheQueries' => false,
)
);


This works fine, *but* I'm wondering what will happen when I am not
able to use contain...is there a fix to the table_prefix problem when
we use joins?.

I would appreciate any help :-).

Cheers!

--
Milton

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: