开发者

Problem with CakePHP model joins, missing fields from joined table.

Im probably missing something painfully obvious here. Im trying to join multiple tables together in Cake, but Im only getting the fields from the first table. Consider the following code..

$joins = array();
$joins[] =  array(
            'table' => 'products', 
            'alias' => 'Product', 
            'type' => 'LEFT',
            'conditions' => array(
                'Device.id = Product.device_id'
            )
    );

$joins[] =  array(
            'table' => 'pricepoints', 
            'alias' => 'Pricepoints', 
            'type' => 'LEFT',
            'conditions' => array(
                'Pricepoints.product_id = Product.id'
            )
    ); 
$all_products = $this->Device->find('all', array("joins" => $joins); 

And this returns the following SQL

SELECT `Device`.`id`, `Device`.`manufacturer_id`, `Device`.`name`, `Device`.`type_id`, `Manufacturer`.`id`, `Manufacturer`.`name` FROM `devices` AS `Device` LEFT JOIN products AS `Product` ON (`Device`.`id` = `Product`.`device_id`) LEFT JOIN pricepoints AS `Pricepoints` ON (`Pricepoints`.`product_id` = `Product`.`id`) LEFT JOIN `manufacturers` AS `Manufacturer` ON (`Device`.`manufacturer_id` = `Manufacturer`.`id`) 

ie. it only returns the开发者_StackOverflow社区 fields from the parent Model ie. Device. How do I get it to select ALL fields from the join? Im presuming its to do with the way I have my Model relationships set up, but I think I have these set up correctly.

Anyone any advice?


you can specify the fields in the find query:

$all_products = $this->Device->find('all', array("fields" => array('Device.*','Product.*','Pricepoints.*')
                                                 "joins" => $joins
); 

Hope this helps


Do your join by calling bindModel() instead of manually specifying the join. See the cookbook for the details on creating and destroying associations on the fly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜