在cakephp中连接两个表时,只选择一个表的数据 [英] Only one table's datas selected when join two tables in cakephp
本文介绍了在cakephp中连接两个表时,只选择一个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码,但只选择了一个表的数据。
I used this code, but only one table's data selected.
我需要从两个表中选择所有字段:
I need to select all fields from two tables:
$options['joins'] = array(
array('table' => 'tbl_users',
'alias' => 'Users',
'type' => 'INNER',
'foreignKey' => 'assigned_by',
'fields' => 'Users.user',
'conditions' => array(
'TravancoAdmin.assigned_by = Users.id'
)
)
);
$options['conditions'] = array( 'TravancoAdmin.id' => $task_id);
$result = $this->find('all', $options);
return $result ? $result : 1;
如何获取两个表中的所有字段?
How can i get all fields from two tables?
如果我的代码有任何错误?
If there is any mistakes in my code?
推荐答案
您需要移动
选项。否则$ this-> find将只获取当前模型的字段。当您指定'joins'
中的字段''fields'
时,请记住添加所需的所有字段,包括当前模型的字段和您要加入的表格中需要的字段。
You need to move the 'fields'
option out of 'joins'
. Otherwise $this->find will only fetch the fields for the current model. Remember to add all fields you need when you specify 'fields'
, both the ones for the current model and the ones you need from the table you're joining.
例如:
$options['joins'] = array(
array('table' => 'tbl_users',
'alias' => 'Users',
'type' => 'INNER',
'foreignKey' => 'assigned_by',
// 'fields' => 'Users.user', <- get rid of this
'conditions' => array(
'TravancoAdmin.assigned_by = Users.id'
)
)
);
$options['fields'] = array('TheModelThatThisRefersTo.*','Users.user'); // <- insert this
$options['conditions'] = array( 'TravancoAdmin.id' => $task_id);
$result = $this->find('all', $options);
return $result ? $result : 1;
这篇关于在cakephp中连接两个表时,只选择一个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文