CakePHP-代码$hasOne的结果
我有个问题,希望不要把你弄糊涂 我想显示文章的用户名(它们是两个表,用户和文章), 相关性是users.id->posts.author(外键) 这将正确显示我想要的内容:CakePHP-代码$hasOne的结果,php,mysql,cakephp,Php,Mysql,Cakephp,我有个问题,希望不要把你弄糊涂 我想显示文章的用户名(它们是两个表,用户和文章), 相关性是users.id->posts.author(外键) 这将正确显示我想要的内容: SELECT `Post`.`id`, `Post`.`author`, `Post`.`title`, `Post`.`content`, `Post`.`description`, `Post`.`status`, `Post`.`name`, `Post`.`type`, `Post`.`date`, `Post`.`
SELECT `Post`.`id`, `Post`.`author`, `Post`.`title`, `Post`.`content`, `Post`.`description`, `Post`.`status`,
`Post`.`name`, `Post`.`type`, `Post`.`date`, `Post`.`modified`, `Post`.`img`,
`Authors`.`username`, `Authors`.`id`
FROM `myblog`.`posts` AS `Post`
LEFT JOIN `myblog`.`users` AS `Authors` ON (`Authors`.`id` = `Post`.`author`)
WHERE `Post`.`type` = 'post'
ORDER BY `Post`.`date` desc
LIMIT 5
问题是下面的代码在
(`Authors`.`id` = `Post`.`author`)
皈依
ON (`Authors`.`id` = `Post.author`)
导致“Post”`作者必须是字符串
cakePHP中的代码
后模型内部
在postsController内部
返回所有结果,但用户名为空
你不知道吗?
使用子查询
我试图转义字符'conditions'=>array('Authors.'id'=>'Post.author'),但没有成功。
我认为问题在于关系类型。帖子属于一个用户,但一个用户可以有多篇帖子。因此,这种关系应该是- User.php
public $hasMany = array('Post');
public $belongsTo = array('User');
Post.php
public $hasMany = array('Post');
public $belongsTo = array('User');
谢谢Fazal Rasel:)我错了,是‘foreignKey’=>‘author’,而不是我的‘foreignKey’=>‘id’:P等人与贝朗斯托一起工作