3

我正在为我的项目使用 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 视图中,而不是类别视图中。

模型关系:

类别:

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' => ''
    )
);

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

4

2 回答 2

4

看到这个:包含更深层次的关联

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

 $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')
                    )
                )
            )
        );
于 2013-01-23T23:22:53.173 回答
0

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

$this->ModelName->unbindModel(array
(
    'hasMany' => array
    (
        'OtherModel'
    )
));

$this->ModelName->bindModel(array
(
    'belongsTo' => array
    (
        'SomeOtherModel' => array
        (
            'foreignKey' => false,
            'conditions' => array
            (
                'ModelName.id = SomeOtherModel.foreignKey'
            )
        )
    )
));
于 2013-01-24T05:40:49.880 回答