【问题标题】:CakePHP: findById() with Containable not returning expected associationsCakePHP:带有 Containable 的 findById() 未返回预期的关联
【发布时间】:2013-05-02 20:52:26
【问题描述】:

这是我的模型:

class Question extends AppModel {


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

public $hasOne = array(
    'CaseStudy' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);

这是我的控制器中的操作:

public function admin_delete_case_question($question_id) {
        $this->Question->Behaviors->load('Containable');
        $this->Question->contain( array('CaseStudy'));
        $question = $this->Question->findById($question_id );
        debug($question); 
        exit;

上面的调试返回如下内容:

array(
    'Question' => array(
        'id' => '78',
        'nickname' => '',
        'content' => 'sdgsdfgs',
        'type' => 'CQ',
        'option1' => 'sdfgsdfg',
        'option2' => '',
        'option3' => '',
        'option4' => '',
        'time' => '-1',
        'difficulty' => '0.0000',
        'slope' => '0.0000',
        'chance' => '0',
        'experiment' => false,
        'created' => '2013-05-02 16:30:29',
        'modified' => '2013-05-02 16:30:29',
        'status' => null,
        'perm_id' => '76',
        'notes' => null,
        'is_deleted' => false,
        'answer_id' => '0',
        'parent_id' => '77',
        'order' => null
    ),
    'CaseStudy' => array(
        'id' => null,
        'nickname' => null,
        'content' => null,
        'type' => null,
        'option1' => null,
        'option2' => null,
        'option3' => null,
        'option4' => null,
        'time' => null,
        'difficulty' => null,
        'slope' => null,
        'chance' => null,
        'experiment' => null,
        'created' => null,
        'modified' => null,
        'status' => null,
        'perm_id' => null,
        'notes' => null,
        'is_deleted' => null,
        'answer_id' => null,
        'parent_id' => null,
        'order' => null
    )
)

我不明白为什么 CaseStudy 数组全部为 NULL,因为当我查看数据库中的该记录 (id 77) 时,所有数据都是正确的。我做错了什么?

【问题讨论】:

    标签: php mysql cakephp cakephp-2.0 cakephp-2.1


    【解决方案1】:

    在您的示例中...问题有一个案例研究;这意味着问题是父母。

    根据您的数据,您的问题有一个 parent_id,所以我假设您实际上是说 CaseStudy 是父级,问题是子级。看起来您的关联实际上是倒退的(通常父级不会有 parent_id)。

    将您的 hasOne 关联替换为 belongsTo:

    public $belongsTo = array(
          'CaseStudy' => array('className'=>'Question', 'foreignKey'=>'parent_id')
    );
    

    这将确保 CaseStudy 是父级,而 Question 是子级。

    按照目前的设置方式,CakePHP 正在尝试寻找其 parent_id 等于您的 Question.id 的 CaseStudy;因为您的 DB CakePHP 中没有任何类似的数据,所以什么也没找到并返回空值。 实际上,CaseStudy 实际上是父级,并且 Question 的 parent_id 等于 CaseStudy.id

    【讨论】:

    • 这很有意义。我马上去测试。但是,我想澄清一件事:CaseStudy 类也是 Question。换句话说,我在同一个表/模型中的记录之间有 belongsTo 和 hasManuy 关联。也许这从问题中很明显,但我想确保它很清楚,以防这需要特殊处理。
    • 是的,我明白 - 我昨晚实际上为此创建了自己的测试环境,以验证它是否适合您,并且在我的测试中它按预期工作。
    • 也为我工作!谢谢!!
    【解决方案2】:

    试试这个:

    //Question model
    public $actsAs = array('Containable'); //personally, I put this in my AppModel
    
    //QuestionsController
    $question = $this->Question->find('first', array(
       'conditions' => array(
            'id' => $question_id
        ),
        'contain' => array(
            'CaseStudy'
        )
    ));
    

    【讨论】:

    • 我从书中得到了这个想法:book.cakephp.org/2.0/en/core-libraries/behaviors/… 它在第一段中说你可以动态加载行为,就像我在这里和许多其他地方所做的那样。
    • 用你的方法确定,结果是一样的(CaseStudy all null values)
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2012-07-06
    • 2011-12-29
    • 1970-01-01
    • 2017-01-15
    • 2018-02-05
    • 2011-09-11
    相关资源
    最近更新 更多