CakePHP具有与预期不一样的链接
我有三个表:用户、配置文件和朋友 它们是这样连接的:CakePHP具有与预期不一样的链接,php,cakephp,Php,Cakephp,我有三个表:用户、配置文件和朋友 它们是这样连接的: public $name = 'User'; public $hasOne = 'Profile'; public $hasMany = array( 'Post', 'Answer' ); public $hasAndBelongsToMany = array( 'User'=>array( 'className'
public $name = 'User';
public $hasOne = 'Profile';
public $hasMany = array(
'Post',
'Answer'
);
public $hasAndBelongsToMany = array(
'User'=>array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
而profile模型只是一个belongsTo用户,所以我没有展示,而friends模型是虚拟的,通过在用户模型中进行关联
因此,要为用户获取朋友列表。我在friends controller中按如下方式传递用户名:
public function index( $username )
{
$friends = $this->User->find('first',
array(
'conditions'=>array(
'User.username'=>$username
),
'contain'=>array(
'Profile',
'Friend'=>array(
'conditions'=>array(
'Friend.status'=>1
),
'contain'=>'Profile'
)
)
)
);
$this->set('friends', $friends);
}
这应该拉的朋友和有关联的个人资料信息的数据
但是我得到了这个错误:Model“Friend”与Model“Profile”没有关联[APP/Cake/Model/Behavior/ContainableBehavior.php,第339行]
所以有些东西工作不正常
有人能帮忙吗
我试着在模型中将he Hasand BelongTomany的名字改为Friends,但这会导致一个错误,即表格没有被取消。。。所以这也不是解决办法
表:
**users**
id
username
password
**profiles**
id
name
user_id
**friends**
id
user_from
user_to
status
如果我将模型更改为:
public $hasAndBelongsToMany = array(
'Friend'=>array(
'className' => 'Friend',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
然后我得到这个错误:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Friend'
SQL Query: SELECT `Friend`.`id`, `Friend`.`user_from`, `Friend`.`user_to`, `Friend`.`created`, `Friend`.`status`, `Friend`.`id`, `Friend`.`user_from`, `Friend`.`user_to`, `Friend`.`created`, `Friend`.`status` FROM `db52704_favorr`.`friends` AS `Friend` JOIN `db52704_favorr`.`friends` AS `Friend` ON (`Friend`.`user_from` = 6 AND `Friend`.`user_to` = `Friend`.`id`)
相关模型的模型和别名应是唯一的。在您的情况下,问题是:
public $name = 'User';
...
public $hasAndBelongsToMany = array(
'User'=>array(
...
如果联接表名为friends
,则可以使用friends
作为好友用户的别名
第二件事是您试图同时包含User=>Profile
和User=>Friend=>Profile
,这可能也是有问题的
根据我的经验,deep
contain
s可能效率很低。可以考虑从好友中删除好友的<代码>配置文件< /代码>,然后分别提取好友用户ID和获取配置文件。相关模型的别名和别名应该是唯一的。在您的情况下,问题是:
public $name = 'User';
...
public $hasAndBelongsToMany = array(
'User'=>array(
...
如果联接表名为friends
,则可以使用friends
作为好友用户的别名
第二件事是您试图同时包含User=>Profile
和User=>Friend=>Profile
,这可能也是有问题的
根据我的经验,deep
contain
s可能效率很低。你可以考虑从好友中删除好友的<代码>配置文件< /代码>,然后分别提取好友用户ID和获取配置文件。 你的<代码>朋友< /C>模式没有<代码>用户标识,这就是为什么代码>用户< /代码>模型不能与<代码>朋友< /代码>联系。尝试以下方法:
public function index( $username )
{
$this->User->Behaviors->attach('Containable');
$friends = $this->User->find('first',
array(
'conditions'=>array(
'User.username'=>$username
),
'contain'=>array(
'Profile',
'Friend'=>array(
'conditions'=>array(
'Friend.status'=>1
)
)
)
)
);
$this->set('friends', $friends);
}
您的
朋友
模型没有用户id
,这就是用户
模型无法与朋友
联系的原因。尝试以下方法:
public function index( $username )
{
$this->User->Behaviors->attach('Containable');
$friends = $this->User->find('first',
array(
'conditions'=>array(
'User.username'=>$username
),
'contain'=>array(
'Profile',
'Friend'=>array(
'conditions'=>array(
'Friend.status'=>1
)
)
)
)
);
$this->set('friends', $friends);
}
我想你正在寻找这样的东西: User.php
public $hasAndBelongsToMany = array(
'Friend'=>array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
这将在habtm关系中将用户表别名为其自身的朋友。为了保持惯例,应该使用user\u id
和friend\u id
键调用联接表friends\u users
以下是我制作的测试应用程序的结果(只需添加您的条件):
我想你正在寻找这样的东西: User.php
public $hasAndBelongsToMany = array(
'Friend'=>array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
这将在habtm关系中将用户表别名为其自身的朋友。为了保持惯例,应该使用user\u id
和friend\u id
键调用联接表friends\u users
以下是我制作的测试应用程序的结果(只需添加您的条件):
这个问题是在使用Friend作为别名而不是用户时发生的,与您所说的完全相反。无论如何,我仍然不理解这里的问题:/n您提供的用户模型没有
Friend
关联,但您的contain子句确实包含Friend
。怎么会?我的建议是尽量避免使用唯一的别名。你能举个例子说明你的意思以及我如何解决这个问题吗,就像你看到我更新的帖子一样,我尝试使用Friend,但它会导致显示的错误。。。所以我不知道该怎么办。哦,我的意思是试试public$hasandbelongtomany=array('Friend'=>array)('className'=>'User'
。您不需要habtm的朋友
模型。当使用朋友作为别名而不是用户时,问题就发生了,实际上与您所说的相反。无论如何,我仍然不理解这里的问题:/您提供的用户模型没有朋友
关联是的,但是你的contain子句确实包含了Friend
。为什么呢?我的建议是尽量避免使用唯一的别名。你能举一个例子说明你的意思和我如何解决这个问题吗,就像你看我更新的帖子一样,我尝试使用Friend,但它导致了显示的错误……所以不确定该怎么做……哦,我的意思是尝试public$hasAndBelONGTOMANY=数组('Friend'=>数组('className'=>'User'
。您不需要habtm的朋友
模型。也不起作用!如果调试$friends,它只提取该用户的用户和配置文件信息,而不是朋友。也不起作用!如果调试$friends,它只提取该用户的用户和配置文件信息,而不是用户的信息。)他是我的朋友。它可能对你来说已经过时了,但对将来的其他人可能会有帮助。它可能对你来说已经过时了,但对将来的其他人可能会有帮助。