Model CakePHP没有来自相关模型的数据
我有两个相互关联的模型Model CakePHP没有来自相关模型的数据,model,relationship,cakephp-2.1,Model,Relationship,Cakephp 2.1,我有两个相互关联的模型 class AccountModel extends AppModel { public $name = 'Account'; public $belongsTo = array( 'User' => array( 'className' => 'User', 'foreignKey' => 'user_id', ), ); } class
class AccountModel extends AppModel {
public $name = 'Account';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
),
);
}
class UserModel extends AppModel {
public $name = 'User';
public $hasMany = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'user_id',
),
);
}
表:
账户
-身份证
-用户id
-其他领域
使用者
-身份证
-其他领域
从一个模型请求数据只返回该模型的数据,而不返回相关数据。
我想请求id为的模型用户,并返回所有用户数据和相关帐户数据。
与使用一个参数(而不是帐户id或用户id)请求帐户模型相同,并返回所有帐户数据和相关用户数据
$User = $this->Account->findByField($param);
仅返回数组'Account'=>数组。。。及
$User = $this->User->findById($id);
仅返回数组'User'=>数组。。。但是没有请求返回数组'User'=>array…,'Account'=>array
如何获取所有相关数据
问候
m、 FindBy{fieldname}不允许使用cakephp文档findBystring$value[,mixed$fields[,mixed$order]]中定义的递归选项
为了通过关系获取递归数据,应该使用find方法
$userData = $this->User->find('first', array(
'conditions' => array('id' => $userId),
'recursive' => 1,
));
如果未返回任何数据,请检查以下内容:
-是否正在传递有效的用户标识?
-两个表中是否都有给定用户ID的数据?
-在您试图运行查找查询的地方,模型是否正确加载?检查$this->Account->recursive=2;没有效果,与$this->Account->find'first',array'conditions'=>array'field'=>$param',recursive'=>2相同;转到并搜索saveAll$数据。这是一种保存记录及其相关记录的方法,这正是您想要的。祝你好运这正是我想要的?我想找到,而不是保存/安装CakePHP的调试工具包工具栏,并查看为查询生成的SQL。