CakePHP模型加入,缺少字段 [英] CakePHP model JOINS, missing fields
本文介绍了CakePHP模型加入,缺少字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,我是CakePHP的新手,在连接两个表时遇到问题。
Hey guys I am new to CakePHP and I am having a problem while joining two tables.
$options['joins'] = array(
array('table' => 'options',
'alias' => 'Options',
'type' => 'LEFT',
'fields' => array('Question.question', 'Options.option'),
'conditions' => array(
'Options.question_id = Question.id'))
);
$this->set('qq',$this->Question->find('all',$options));
我只得到问题表的字段,而不是选项表的字段。我如何也获得选项表的字段?
I am getting only the fields of question table and not the fields of options table. How do I get the fields of options table also?
推荐答案
尝试绑定表。
$this->ModelName->bindModel(array
(
'belongsTo' => array
(
'ModelToJoin' => array
(
'foreignKey' => false,
'conditions' => array
(
'ModelToJoin.model_name_id = ModelName.id'
)
)
)
));
根据您的情况。
<?php
$this->Question->bindModel(array
(
'hasMany' => array
(
'Options' => array
(
'foreignKey' => false,
'type' => 'LEFT',
'conditions' => array
(
'Options.question_id = Question.id'
)
)
)
));
$this->Question->find('all',array
(
'fields' => array('Question.question', 'Options.option')
));
这篇关于CakePHP模型加入,缺少字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文