【问题标题】:How to use inner join in CakePHP models如何在 CakePHP 模型中使用内连接
【发布时间】:2014-02-24 21:28:40
【问题描述】:

我有两张桌子要INNER JOIN,我花了几个小时但没有运气。如果有人可以提供帮助,我将非常高兴。

我的第一张桌子:properties

id | room | price | location_id

我的第二张桌子是:locations

id | country | province | district

注意:Properties 中的 location_idLocation 中的“id”

我想使用hasOnebelongsTo 等基本模型关联。

我应该在我的模型中放入什么,以便我基本上可以得到以下结果?

SELECT
    Property.room,
    Property.price,
    Location.province
FROM
    properties AS Property
INNER JOIN locations AS Location ON Property.location_id = Location.id

提前致谢!

【问题讨论】:

  • 显示你的模型定义

标签: cakephp join model associations model-associations


【解决方案1】:

以下模型关系将生成您需要的查询。

class Property extends AppModel {

    public $belongsTo = array(
        'Location' => array(
            'type' => 'INNER'
        )
    );

}

阅读更多关于model associations

【讨论】:

    【解决方案2】:

    使用以下代码:

    $this->Property->find('all', array('joins' => array(array('table' => 'locations',
                                       'alias' => 'Location',
                                       'type' => 'INNER',
                                       'conditions' => array('Property.LOCATION_ID = Location.ID')))));
    

    【讨论】:

      【解决方案3】:

      试试这个

      class Location extends AppModel {
      
          public $belongsTo = array('Property'));
      
      }
      
      class Property extends AppModel {
      
          public $hasOne = array( 'Location'));
      
      }
      

      【讨论】:

        【解决方案4】:

        试试这个:

        class Variant extends AppModel {
        
            $this->bindModel(array('belongsTo' => array('Brand' => array('type' => 'INNER'))));
        
        }
        

        【讨论】:

          【解决方案5】:

          CakePHP 确实支持定义您自己的连接的语法。从PropertiesController

          $properties = $this->Property->find('all', array(
                      'fields' => array('Property.*'),
                      'joins' => array(
                          array(
                                  'table' => 'locations',
                                  'alias' => 'Location',
                                  'type' => 'INNER',
                                  'conditions' => array(
                                  'Property.location_id' =>'Location.id'
                                  )
                              ),
                          ),
                      )
                  );
          

          另一种方法

          您将与您的模型创建关系,并使用如下可包含的行为:

          class Property extends AppModel {
              public $actsAs = array('Containable');
              public $belongsTo = array('Property');
          }
          
          class Location extends AppModel {
              public $actsAs = array('Containable');
              public $hasMany = array('Property');
          }
          

          然后你可以通过PropertiesController

          $properties = $this->Property->find('all', array(
              'contain' => array('Location')
          ));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多