从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-29It 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条件
发布评论