How do I write a join query across multiple tables in CakePHP?

后端 未结 6 1597
既然无缘
既然无缘 2020-11-28 07:50

can anyone tell me, how to retrieve joined result from multiple tables in cakePHP ( using cakePHP mvc architecture). For example, I have three tables to join (tbl_topics, tb

相关标签:
6条回答
  • 2020-11-28 08:11
    $markers = $this->Marker->find('all', array('joins' => array(
        array(
            'table' => 'markers_tags',
            'alias' => 'MarkersTag',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions'=> array('MarkersTag.marker_id = Marker.id')
        ),
        array(
            'table' => 'tags',
            'alias' => 'Tag',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions'=> array(
                'Tag.id = MarkersTag.tag_id',
                'Tag.tag' => explode(' ', $this->params['url']['q'])
            )
        )
    ))); 
    

    as referred to in nate abele's article: link text

    0 讨论(0)
  • 2020-11-28 08:16

    I'll be honest here and say that you'll probably be a lot happier if you just create a function in your model, something like getTopicVotes() and calling query() there. Every other solution I can think of will only make it more complicated and therefore uglier.

    Edit:

    Depending on the size of your data, and assuming you've set up your model relations properly (Topic hasMany Items hasMany Votes), you could do a simple find('all') containing all the items and votes, and then do something like this:

    foreach ($this->data as &$topic)
    {
        $votes = Set::extract('/Topic/Item/Vote', $topic);
        $topic['Topic']['vote_count'] = count($votes);
    }
    

    Two things are important here:

    1. If you have a lot of data, you should probably forget about this approach, it will be slow as hell.
    2. I've written this from my memory and it might not look like this in real life and/or it may not work at all :-)
    0 讨论(0)
  • 2020-11-28 08:21

    You should study HaBTM (Has and Belongs to Many) http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

    0 讨论(0)
  • 2020-11-28 08:25

    What you need is recursive associations support, which is not possible with stock CakePHP currently.
    Although it could be achieved using some bindModel trickery
    or an experimental RecursiveAssociationBehavior.

    Both of these solutions will either require you to use extra code or rely on a behaviour in your application but if you resist the temptation to write pure SQL code, you'll be rewarded with being able to use Cake`s pagination, auto conditions, model magic etc..

    0 讨论(0)
  • 2020-11-28 08:27

    You can easily set the "recursive" property on a find() query.

    $result = $this->Topic->find('all', array('recursive' => 2));
    

    Alternatively, you can use the Containable behavior in your model. Then you can use:

    $this->Topic->contain(array(
        'Item',
        'Item.Vote',
    ));
    
    $result = $this->Topic->find('all');
    

    or

    $result = $this->Topic->find('all', array(
        'contain' => array(
            'Item',
            'Item.Vote',
        ),
    ));
    
    0 讨论(0)
  • 2020-11-28 08:28

    I think this answer is already submitted, but I am posting here for someone who seeks still for this. The joins can be done with find() method can be like below

    $result = $this->ModelName1->find("all",array(
    'fields' => array('ModelName1.field_name','Table2.field_names'), // retrieving fileds 
    'joins' => array(  // join array
        array(
            'table' => 'table_name',
            'alias' => 'Table2',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions'=> array('ModelName1.id = Table2.id') // joins conditions array
        ),
        array(
            'table' => 'table_name3',
            'alias' => 'Table3',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions'=> array('Table3.id = Table2.id')
        )
    ))); 
    
    0 讨论(0)
提交回复
热议问题