CakePHP包含和belongsTo hasMany条件

编程入门 行业动态 更新时间:2024-10-25 10:34:00
本文介绍了CakePHP包含和belongsTo hasMany条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从CakePHP手册:

假设您在Posts和Authors之间有belongsTo关系。假设您想要查找包含特定关键字(魔术)或过去两周内创建的所有帖子,但是您希望将搜索范围限制为由Bob编写的帖子: array (Author.name=>Bob,OR=>数组(Post.title LIKE=>%magic%,Post.created>=> date('Ym-d',strtotime( - 2 weeks))))

然而,它并不适合我。我有Scrapes属于Vargroup和Vargroup hasMany刮。我的scrapes表有一个字段vargroup_id

当我这样做时:

$这 - > Vargroup->包含( '刮'); $ this-> Vargroup-> find('first');

我得到:

数组( [Vargroup] =>数组( [id] => 16950 [item_id] => 1056 [image] => cn4535d266.jpg [price] => 22.95 [instock] => 1 [timestamp] => 2012-10-29 11:35:10 ) [Scrape] =>数组( [0] =>数组( [id] => 18163 [item_id] => 1056 [vargroup_id] => 16950 [timestamp] => 2012-05-23 15:24:31 $ [b] $ b pre>

但是当我这样做时:

$ this-> Vargroup - >包含( '刮'); $ this-> Vargroup-> find('first',array('conditions'=> array('not'=> array( array ('Scrape.timestamp>'=> $ today)))));

我得到以下错误与关联的sql输出

警告(512):SQL错误:1054:'where子句'中的未知列'Scrape.timestamp'查询:SELECT`Vargroup`.`id`, Vargroup`.`item_id`,`Vargroup`.`image`,`Vargroup`.`price`,`Vargroup`.````股东,`Vargroup`.`timestamp`从`vargroups`由于`Vargroup`在哪里`Scrape` 。'timestamp` ='2012-10-29

它看起来不像它绑定的表在所有.. 任何帮助,将不胜感激。 Thanks $ / $>

解决方案

试试这个: <$ p $包含('Scrape');< code> $ this-> $ this-> Vargroup-> find('first',array('conditions'=> array('not'=> array(' Scrape.timestamp>'=> array($ today)))));

我认为这个来自cakephp页面的例子告诉了正确的方式来做一个不的条件, $ check out。 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$'$' >数组(First post,Second post,Third post)))

如果您有更多问题,这是复杂的查找条件页面。 Cakephp复杂查找条件

From the CakePHP manual:

Let's say you had a belongsTo relationship between Posts and Authors. Let's say you wanted to find all the posts that contained a certain keyword ("magic") or were created in the past two weeks, but you want to restrict your search to posts written by Bob: array ( "Author.name" => "Bob", "OR" => array ( "Post.title LIKE" => "%magic%", "Post.created >" => date('Y-m-d', strtotime("-2 weeks")) ) )

However, its not working for me. I've got Scrapes belongsTo Vargroup, and Vargroup hasMany Scrape. My scrapes table has a field vargroup_id

When I do:

$this->Vargroup->contain('Scrape'); $this->Vargroup->find('first');

I get:

Array ( [Vargroup] => Array ( [id] => 16950 [item_id] => 1056 [image] => cn4535d266.jpg [price] => 22.95 [instock] => 1 [timestamp] => 2012-10-29 11:35:10 ) [Scrape] => Array ( [0] => Array ( [id] => 18163 [item_id] => 1056 [vargroup_id] => 16950 [timestamp] => 2012-05-23 15:24:31 [instock] => 1 [price] => 22.95 ) ) )

But when I do :

$this->Vargroup->contain('Scrape'); $this->Vargroup->find('first', array( 'conditions' => array( 'not' => array( array('Scrape.timestamp >' => $today) ) ) ));

Im getting The following error with associated sql output

Warning (512): SQL Error: 1054: Unknown column 'Scrape.timestamp' in 'where clause' Query: SELECT `Vargroup`.`id`, `Vargroup`.`item_id`, `Vargroup`.`image`, `Vargroup`.`price`, `Vargroup`.`instock`, `Vargroup`.`timestamp` FROM `vargroups` AS `Vargroup` WHERE `Scrape`.`timestamp` = '2012-10-29

It doesn't look like its binding the table at all.. Any help would be appreciated. Thanks

解决方案

Try with this:

$this->Vargroup->contain('Scrape'); $this->Vargroup->find('first', array( 'conditions' => array( 'not' => array( 'Scrape.timestamp >' => array($today) ) ) ));

i think this examples from cakephp page tell the right way to do a "not" condition, check it out.

array( "NOT" => array("Post.title" => array("First post", "Second post", "Third post")) )

this is the complex find conditions page if you have more questions. Cakephp complex find conditions

更多推荐

CakePHP包含和belongsTo hasMany条件

本文发布于:2023-10-21 17:19:46,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   CakePHP   belongsTo   hasMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!