In cakephp how can I do a find with conditions on a related field?

前端 未结 4 1586
独厮守ぢ
独厮守ぢ 2020-12-18 15:33

I\'ve got a model (Listings) that has and belongs to a few different models, I\'d like to find all of this model where it\'s related model (Openhouses) have a condition. The

相关标签:
4条回答
  • 2020-12-18 16:17

    If you have a lot of linked models, settings recursive to 2 might bring more data than you might want.

    If that is true, there is an alternative to what mavarro said, you can also try using the Containable behaviour:

    $this->Listing->find
    (
        'all', 
        array
        (
            'conditions' => array
            (
                'Openhouse.date >' => $openhouse_start->format('Y-m-d H:i:s'),
                'Openhouse.date <' => $openhouse_end->format('Y-m-d H:i:s')
            ),
            'contain' => array('Openhouse')
        )
    );
    
    0 讨论(0)
  • 2020-12-18 16:19

    Try this:

    $this->List->find('all', array(
        'contain' => array(
            'Openhouse.conditions' => array(
                'Openhouse.date >' => $openhouse_start->format('Y-m-d H:i:s'),
                'Openhouse.date <' => $openhouse_end->format('Y-m-d H:i:s'))
            )
        )
    )
    
    0 讨论(0)
  • 2020-12-18 16:28
    $this->List->find('all', array(
        'contain' => array(
            'conditions' => array(
                'Openhouse.date >' => $openhouse_start->format('Y-m-d H:i:s'),
                'Openhouse.date <' => $openhouse_end->format('Y-m-d H:i:s')
                                 ),
            'order' => array('Openhouse.date DESC')
                          )
                                 )
    )
    
    0 讨论(0)
  • 2020-12-18 16:30

    Try adding a $this->Listing->recursive = 2; before the call to find all. That should link up the tables before the call giving you access to the Openhouse model from the Listing model.

    0 讨论(0)
提交回复
热议问题