2012-11-14 44 views
0

從CakePHP的手冊:CakePHP的遏制和屬於關聯的hasMany條件

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")) 
) 
) 

然而,它不適合我的工作。我有Scrapes belongsTo Vargroup,Vargroup hasMany Scrape。我的刮傷表有一個字段vargroup_id

當我這樣做:

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

我得到:

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 
      ) 
    ) 
) 

但是當我做:

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

即時得到下面的誤差關聯sql輸出

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 

它看起來不像它綁定桌子.. 任何幫助,將不勝感激。 感謝

回答

0

試試這個:

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

我覺得從CakePHP的頁面這個例子告訴做一個「不」條件的正確方法, 檢查出來。

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

這是複雜的查找條件頁面,如果您有更多的問題。 Cakephp complex find conditions

+0

我仍然收到相同的錯誤。雖然我看到你確實修復了一個與額外數組有關的錯誤。謝謝。 –

+0

coukld你給我們更多的信息,如表名,發佈模式等... – Eefret