【问题标题】:cake php contain in contain蛋糕php包含在包含
【发布时间】:2013-01-07 11:55:05
【问题描述】:

我正在为我的项目使用 CakePHP... 为了加快我的代码,我只想获得一些与我的实际对象相关的模型:

$result = $this->Category->find(
                'first',
                array(
                    'conditions' => array(
                        'Category.folder' => $path[0]
                    ),
                    'contain' => array('Rubric',
                        'conditions' => array('Rubric.category_id' => 'Category.id'),
                        'contain' => array('Subrubric',
                            'conditions' => array('Subrubric.rubric_id' => 'Rubric.id')
                        )
                    )
                )
            );

path[0] 是来自 url 的参数...

找到了类别和评分准则,但没有找到子评分准则。还有一些与我的对象相关的条目,但我希望它们在我的 rubric-view 中而不是在 category-view 中。

模型关系:

类别:

public $hasMany = array(
        'Rubric' => array(
            'className' => 'Rubric',
            'foreignKey' => 'category_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => 'title',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

评分标准:

public $hasMany = array(
    'Entrieslocation' => array(
        'className' => 'Entrieslocation',
        'foreignKey' => 'rubric_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
    'Subrubric' => array(
        'className' => 'Subrubric',
        'foreignKey' => 'rubric_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

这里我不想要入口位置...

【问题讨论】:

    标签: php cakephp model database-relations


    【解决方案1】:

    看到这个:Containing deeper associations

    您不应该在更深层次的关系中使用节点“包含”。您的代码应如下所示。

     $result = $this->Category->find(
                'first',
                array(
                    'conditions' => array(
                        'Category.folder' => $path[0]
                    ),
                    'contain' => array('Rubric',
                        'conditions' => array('Rubric.category_id' => 'Category.id'),
                        'Subrubric' => array(
                            'conditions' => array('Subrubric.rubric_id' => 'Rubric.id')
                        )
                    )
                )
            );
    

    【讨论】:

      【解决方案2】:

      您还可以使用另一种选择。

      $this->ModelName->unbindModel(array
      (
          'hasMany' => array
          (
              'OtherModel'
          )
      ));
      
      $this->ModelName->bindModel(array
      (
          'belongsTo' => array
          (
              'SomeOtherModel' => array
              (
                  'foreignKey' => false,
                  'conditions' => array
                  (
                      'ModelName.id = SomeOtherModel.foreignKey'
                  )
              )
          )
      ));

      【讨论】:

      • 太多的“替代”:)。我一直讨厌绑定和取消绑定模型函数。
      猜你喜欢
      • 2012-05-13
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多