限制多对多将与CakePHP一起使用的深度

时间:2014-06-26 12:38:21

标签: cakephp cakephp-2.1 cakephp-2.3 cakephp-appmodel

对于多对多关系,选择所有人的深度是否有限制?我的设计是这样的:

表调查有许多表调查部分。表调查部分有很多问题。表问题有许多表可能的响应。

如果我在调查表中这样做:

$this->loadModel('Survey');
$this->Survey->recursive = 2;
$getSurvey = $this->Survey->find('all');

它只会到问题表,但会带回可能的响应表。但是,如果我查询Survey Sections表,它会带回一切。这是我的代码的问题还是搜索有多深的限制?

这是我的模特:

<?php
App::uses('AppModel', 'Model');    
class Survey extends AppModel {

    public $validate = array(    
    );

    public $belongsTo = array(    
    );
    public $hasMany = array(
        'surveySection' => array(
            'className' => 'SurveySection',
            'foreignKey' => 'surveys_id',         
        ),
    );

}

<?php
App::uses('AppModel', 'Model');  
class SurveySection extends AppModel {
    public $validate = array(

    );
    public $belongsTo = array(

    );
    public $hasMany = array(
        'Question' => array(
            'className' => 'Question',
            'foreignKey' => 'survey_section_id',
        ),

    );    
}

<?php
App::uses('AppModel', 'Model');    
class Question extends AppModel {
    public $validate = array(

    );
    public $belongsTo = array(

    );
    public $hasMany = array(
        'PossibleResponse' => array(
            'className' => 'PossibleResponse',
            'foreignKey' => 'questions_id',
        ),

    );    
}


<?php
App::uses('AppModel', 'Model');   
class PossibleResponse extends AppModel {
    public $validate = array(

    );
    public $belongsTo = array(

    );
}

1 个答案:

答案 0 :(得分:0)

您应该使用$this->Survey->recursive = 3;

正如我在评论中所说,http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive的例子有点误导,因为它只显示特定案例的例子。事实上,您可以在获取数据时尽可能地去。