php - CakePHP:带有 Containable 的 findById() 未返回预期的关联

标签 php mysql cakephp cakephp-2.0 cakephp-2.1

这是我的模型:

class Question extends AppModel {


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

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

这是我的 Controller 中的操作:

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) 时,所有数据都是正确的。我做错了什么?

最佳答案

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

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

将 hasOne 关联替换为 belongsTo:

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

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

按照目前的设置方式,CakePHP 试图寻找一个 CaseStudy,它的 parent_id 等于您的 Question.id;因为您的数据库中没有任何类似的数据,所以 CakePHP 什么也找不到并返回空值。 实际上,CaseStudy 实际上是父级,而问题的 parent_id 等于 CaseStudy.id

关于php - CakePHP:带有 Containable 的 findById() 未返回预期的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16347519/

相关文章:

mysql - 创建 Elastic Beanstalk 环境时出现 "Connection refused"

mysql - 从表中选择行但尽可能检索覆盖

mysql - 将 CakePHP Web 应用程序的 phpMyAdmin 数据库迁移到 Windows Azure

php - Wordpress 发布自定义社交元标记

php - 活泼的 wkhtmltopdf 包装器将生成的 html 文件发送到浏览器

python - 更新未在 mysql 服务器上提交flask-sqlalchemy

php - 在 CakePHP 中,您使用过的最好的图像上传/调整大小组件是什么?

javascript - 保存进度并提交表单按钮

php - mysql php 无法获取外键值

php - 将 MySQL 查询返回的单列值传递给 PHP 脚本