用用户名cakePHp列出图片的评论 [英] List comments for picture with username cakePhp
问题描述
我的评论有问题。
我有用户模型:
public $ hasMany = array('Photo','Comment');
注释型号:
public $ belongsTo = array(
'User'=> array(
'className'=>'User',
'foreignKey'=>'user_id '
),
'Photo'=> array(
'className'=>'Photo',
'foreignKey'=>'photo_id'
)
);
和照片模型
public $ belongsTo = array(
'User'=> array(
'className'=>'User',
'foreignKey'=>'user_id '
)
);
public $ hasMany = array(
'Comment'=> array(
'className'=>'Comment',
'foreignKey'=> 'photo_id'
)
);
当我读取照片的数据时:
'Photo'=> array - >关于照片的信息对我确定
'User'=> array - >关于用户的信息关于我
'评论'=> array - >所有评论,但只有用户的ID。
在检索照片的相关数据时,如何连接用户的评论获取用户名与评论的相关性? p>
您需要先调用 $ this-> recursive = 2
调用find(),这将获取查询返回的每个对象的所有关联。或者,您可以在任何模型中设置 $ recursive
变量。
不同类型的递归如下:
- -1蛋糕提取照片数据,不加入。
- 0蛋糕提取照片数据
- 1蛋糕提取照片,其域名及其相关评论,和
注释相关的用户
我还建议考虑 Containable ,因为您可以完全控制所有返回的数据,直到指定任何关联中的每个字段
照片型号:
<?php
照片扩展AppModel {
public $ actsAs = array('Containable');
}
您的find方法将如下所示:
<?php
$ this->照片 - > find('all',array(
'contains' & array(
'User',
'Comment'=> array(
'User'= array(
'fields'=> array
),
'conditions'=> array(
'Comment.erased'=> 0
)
))
'conditions' > array(
'Photo.id'=> $ id
)));
I have a problem with my comments. I have User model:
public $hasMany = array('Photo','Comment');
Comment model:
public $belongsTo = array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
),
'Photo'=>array(
'className'=>'Photo',
'foreignKey'=>'photo_id'
)
);
and Photo Model
public $belongsTo = array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
)
);
public $hasMany = array(
'Comment'=>array(
'className'=>'Comment',
'foreignKey'=>'photo_id'
)
);
When I read data of a photo I got:
'Photo' => array -> info about photo which is ok for me
'User' => array -> info about user ok for me
'Comment' => array -> all comments but only with id of users.
How can I connet comments with users to get username related with comment while retrieving data about photo?
You need to call $this->recursive = 2
before you call find(), that will fetch all associations of every object returned by your query. Alternatively you can set the $recursive
variable inside any of your models.
The different types of recursion are as follows:
- -1 Cake fetches Photo data only, no joins.
- 0 Cake fetches Photo data and its domain
- 1 Cake fetches a Photo, its domain and its associated Comments
- 2 Cake fetches a Photo, its domain, its associated Comments, and the Comments’ associated Users
I also suggest considering Containable since you have complete control of all the data that is returned, down to specifying each field in any association
The photo model:
<?php
class Photo extends AppModel {
public $actsAs = array('Containable');
}
And your find method will look like this:
<?php
$this->Photo->find('all', array(
'contain' => array(
'User',
'Comment' => array(
'User' = array(
'fields' => array('username')
),
'conditions' => array(
'Comment.erased' => 0
)
))
'conditions' => array(
'Photo.id' => $id
)));
这篇关于用用户名cakePHp列出图片的评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!