【问题标题】:Cakephp contain and belongsTo hasMany conditionsCakephp contains 和 belongsTo hasMany 条件
【发布时间】:2012-11-03 09:39:18
【问题描述】:

来自 CakePHP 手册:

  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 属于 Vargroup,而 Vargroup 有很多 Scrape。我的 scrapes 表有一个字段 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

它看起来根本不像绑定表格.. 任何帮助,将不胜感激。 谢谢

【问题讨论】:

    标签: cakephp find belongs-to contain


    【解决方案1】:

    试试这个:

     $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

    【讨论】:

    • 我仍然遇到同样的错误。尽管我看到您确实修复了我在使用额外数组时遇到的错误。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多