Join single item from related table based on min of field

偶尔善良 提交于 2019-12-11 19:53:55

问题


I have an Item model which has the following associations:

public $hasOne = array(
    'Project' => array(
        'className' => 'Project',
        'foreignKey' => 'item_id'
    )
);
public $hasMany = array(
    'ItemPic' => array(
        'className' => 'ItemPic',
        'foreignKey' => 'item_id',
        'dependent' => false
    )
);

I am wanting custom data for different views of Item. It seems like CakePHP automatically includes Project data (maybe because it is hasOne?) and does not include the ItemPic data. In the index I really don't even want the Project data... however, I do want the ItemPic data. For each Item record pulled, I want a single ItemPic record joined to it. This ItemPic should be basically ItemPic.item_id = Item.id and ORDER BY ItemPic.rank LIMIT 1.

The purpose of this is basically so that in the index I can show a list of Items and a picture associated with each item. I would like all of the images along with the Project data in the view for a single Item, but not in the list/index.

I was told I could use containable like this:

// In the model
public $actsAs = array('Containable');

// In the controller
$this->paginate = array(
    'conditions' => $conditions,
    'contain' => array(
        'ItemPic' => array(
            'fields' => array('file_name'),
            'order' => 'rank',
            'limit' => 1
        )
    )
);

The above actually works how I want... however, I was also told that doing this would cause an extra query to be ran for every single Item... which I feel I should avoid.

I tried doing this, but I get duplicate data and it doesn't attach any ItemPic data:

    $this->paginate = array(
            'conditions' => $conditions,
            'joins' =>  array(
                    array(
                            'table' => 'item_pics',
                            'alias' => 'ItemPic',
                            'type' => 'LEFT',
                            'conditions' => array(
                                    'ItemPic.item_id = Item.id'
                            ),
                            'order' => 'rank ASC',
                            'limit' => 1
                    )

            )
    );
    $paginated = $this->Paginator->paginate();

回答1:


Can you please try this:

$this->paginate = array(
        'conditions' => $conditions,
        'joins' =>  array(
                array(
                        'table' => 'item_pics',
                        'alias' => 'ItemPic',
                        'type' => 'LEFT',
                        'conditions' => array(
                                'ItemPic.item_id = Item.id'
                        ),
                        'order' => 'rank ASC',
                        'limit' => 1
                )

        ),
    'fields' => array('Item.*','Project.*','ItemPic.*') 
    );

In the fileds section you may or may not assign "Item" , "Project" according to your requirment.

Thanks



来源:https://stackoverflow.com/questions/21897352/join-single-item-from-related-table-based-on-min-of-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!