标签:cakephp-2-0 cakephp-2-1 php mysql cakephp
这是我的模特:
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,因为当我在DB中查看该记录(id 77)时,所有数据都是正确的.我究竟做错了什么?
解决方法:
在你的例子中…问题有一个CaseStudy;这意味着问题是父母.
根据您的数据,您的问题有一个parent_id,所以我假设您实际上意味着CaseStudy是父,而Question是孩子.看起来您的关联实际上是倒退的(通常父节点不会有parent_id).
用belongsTo替换你的hasOne关联:
public $belongsTo = array(
'CaseStudy' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);
这将确保CaseStudy是父级,而Question是孩子.
通过它当前设置的方式,CakePHP正在尝试寻找一个父类与您的Question.id相等的CaseStudy;因为你的数据库中没有任何类似的数据,CakePHP找不到任何内容并返回空值.
实际上,CaseStudy实际上是父,而Question的parent_id等于CaseStudy.id
标签:cakephp-2-0,cakephp-2-1,php,mysql,cakephp 来源: https://codeday.me/bug/20190901/1779639.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。