过滤可容纳模型结果
我正在尝试使用以下代码行通过标签“who”过滤具有某些(不相关)条件的所有帖子
$com= $this->Post->find('all', array('conditions ' => $条件, '包含' => 'Tag.tag="who"'));
然而,结果不仅仅是带有标签的帖子,我得到的每一篇帖子都带有空标签数组,用于那些没有标签“who”的帖子。
我知道我的问题以前曾出现过,但发布的解决方案不适用于我的代码。 我尝试将这里的代码改编为我自己的: http: //web-development-blog.co.uk/2010/09/14/cakephp-habtm-find-with-conditions-and-containable-behavior/,但我收到一条 SQL 错误,指出在 on 子句中找不到“Tag.post_id”。
请帮忙。
尝试从选定链接实现代码时出现错误消息:
SELECT `Post`.`id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Tag`.`id`, `Tag`.`tag`, `Tag`.`created`, `Tag`.`modified` FROM `posts` AS `Post` LEFT JOIN `tags` AS `Tag` ON (`Tag`.`post_id` = `Post`.`id`) WHERE `Tag`.`tag` = 'who' 1054: Unknown column 'Tag.post_id' in 'on clause'
这是由于使用以下内容引起的:
$this->Post->bindModel(array('hasOne' => array('Tag')));
$this->Post->contain(array(
'Tag'
));
$com=$this->Post->find('all', array(
'conditions'=>array('Tag.tag'=>'who')
));
I am trying with the following line of code to filter all the Posts that have certain (irrelevant) conditions, by the tag "who"
$com= $this->Post->find('all', array('conditions' => $conditions, 'contain' => 'Tag.tag="who"'));
However, instead of the results being only the posts with the tags, I get every single post with empty tag arrays for the ones that don't have the tag "who".
I know my question has come up before, but the solutions posted have not worked with my code.
I have tried adapting the code here to my own:
http://web-development-blog.co.uk/2010/09/14/cakephp-habtm-find-with-conditions-and-containable-behavior/, but I get an SQL error stating "Tag.post_id" not found in on clause.
Please help.
Error message when trying to implement code from selected link:
SELECT `Post`.`id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Tag`.`id`, `Tag`.`tag`, `Tag`.`created`, `Tag`.`modified` FROM `posts` AS `Post` LEFT JOIN `tags` AS `Tag` ON (`Tag`.`post_id` = `Post`.`id`) WHERE `Tag`.`tag` = 'who' 1054: Unknown column 'Tag.post_id' in 'on clause'
Which is caused by using this:
$this->Post->bindModel(array('hasOne' => array('Tag')));
$this->Post->contain(array(
'Tag'
));
$com=$this->Post->find('all', array(
'conditions'=>array('Tag.tag'=>'who')
));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误是您使用的是bindModel,但没有说外键或任何内容,您应该在模型中建立关联,并且在绝对必要时使用此即时方法。
你需要告诉cake哪个外键而不是cake将使用默认值,在你的情况下它将尝试Tag.post_id。
另外你应该使用一个有很多关联的或者至少我这么认为。我这么说是因为我认为一个帖子可能有多个标签。
一旦您在模型中进行关联并删除此行
$this->Post->bindModel(array('hasOne' => array('Tag')));
它应该可以完美工作
,或者您可以即时与正确的参数进行关联,请阅读 cookbook 为此,显示如何放置类名的示例是您要查找的,只需添加其他属性
编辑:
抱歉,读错了...有很多关联不进行连接,因此您必须使用 contains 来指定条件或动态向关联添加条件,以下是使用 contains 添加条件后您的代码应如何显示 请
记住包含条件会覆盖关联中的条件!如果您想要额外的条件,则必须动态添加它们,例如:
请记住,动态关联仅适用于该实例。
您还可以为不同类型创建一个 hasmany 别名(记住始终定义类名和外键)。
希望这次能成功,如果不只是发表评论来看看它是否修复;)
Your error is that you are using bindModel wihtout saying the foreignkey or anything, you SHOULD establish the associations in the model and just use this on-the-fly method when is strictly neccesary.
you need to tell cake which foreignkey of not cake will use the default, in your case it will try Tag.post_id.
Also you should use a has many association or at least i think so. I said this beacause i suppose a post may have multiple tags.
once you do the asociation in the model and delete this line
$this->Post->bindModel(array('hasOne' => array('Tag')));
it should work perfectly
OR you can do the association with the correct parameters on the fly read the cookbook for this, the example that shows how to put the classname is the one you seek, just add the other attributes
EDIT:
Sorry, read something wrong... the has many association doesn't do a join so you have to use contain to specify the condition or add a condition to the association on the fly, here is how your code should look after adding the conditions with contain
REMEMBER contain conditions override the conditions in the associations!! if you want to have extra conditions you must add them on the fly something like:
Remember that associations on the fly will work for that instance ONLY.
Also you may do a hasmany alias for different types (remember to define always the classname and foreignkey).
Hope it works this time, if not just comment to see it fix ;)