【问题标题】:CakePHP Custom Search in Multi-Level Model Relations多级模型关系中的 CakePHP 自定义搜索
【发布时间】:2011-12-02 07:54:39
【问题描述】:

我希望有人能对这个问题有所了解。我已经阅读了大量的 stackoverflow 文档、cake 文档、作品,但我似乎无法解决这个问题。我的模型关系如下:

  • 马属于供应商
  • 马属于洛特
  • 供应商有很多马
  • Lo​​t hasMany Sale
  • Lo​​t hasMany Withdrawal

然后,我列出所有批次并显示一个包含马匹、供应商、批次号等的表格。这工作正常,实际上使用 Paginator::Sort 方法我什至可以排序为 Vendor.name ASC 或 DESC ,这告诉我关联是正确的。

但是,我似乎无法根据与属于供应商的任何字段的匹配来操纵搜索结果。例如,如果在我的分页设置中我执行以下操作:

'conditions' => array('Vendor.name' => 'Simon')...

我收到一个 SQL 错误,指出列 Vendor.name 不存在,但如果我删除此条件约束然后评估 SQL,它肯定存在。

还应该说,我正在使用 unbindModel() 和 bindModel() 方法动态创建 hasOne 关系以获得最佳 MySQL 开销,这并没有像以前那样导致手头的问题。

这是我在 var_dump() 分页结果时返回的第一行:

'Vendor' => 
    array
      'id' => string '2' (length=1)
      'name' => string 'ALDORA STUD' (length=11)
      'surname' => string '' (length=0)
      'address' => string '' (length=0)
      'tel' => string '' (length=0)
      'fax' => string '' (length=0)
      'email' => string '' (length=0)
      'url' => string '' (length=0)
      'comments' => string '' (length=0)
      'created' => string '0000-00-00 00:00:00' (length=19)
      'modified' => string '0000-00-00 00:00:00' (length=19)
      'modified_by' => string '0' (length=1)
  'Withdrawal' => 
    array
      'id' => null
  'Sale' => 
    array
      'id' => null
      'catalogue_id' => null
      'lot_id' => null
      'purchaser' => null
      'amount' => null
      'notes' => null
      'date' => null

我省略了很多行,但您可以清楚地看到 Vendor 作为与我正在工作的控制器 Lot 的 hasOne 关系被送回。

如果有帮助,我正在使用此代码取消绑定和重新绑定模型:

        $this->Lot->unbindModel(array(
            'hasOne' => array('Horse'),
            'hasMany' => array('Withdrawal', 'Update', 'Sale'),
            'belongsTo' => array('Catalogue')
        ));

        $this->Lot->Horse->unbindModel(array(
            'belongsTo' => array('Vendor', 'Lot')
        ));

        $this->Lot->Horse->Vendor->unbindModel(array(
            'hasMany' => array('Horse')
        ));

        $this->Lot->bindModel(array(
            'hasOne' => array(
                'Catalogue' => array(
                    'foreignKey' => false,
                    'conditions' => array('Catalogue.id = Lot.catalogue_id')
                ),
                'Horse' => array(
                    'foreignKey' => false,
                    'conditions' => array('Horse.id = Lot.horse_id')
                ),
                'Vendor' => array(
                    'foreignKey' => false,
                    'conditions' => array('Vendor.id = Horse.vendor_id'),
                ),
                'Withdrawal' => array(
                    'foreignKey' => false,
                    'conditions' => array('Withdrawal.lot_id = Lot.id'),
                    'fields' => array('id')
                ),
                'Sale' => array(
                    'foreignKey' => false,
                    'conditions' => array('Sale.lot_id = Lot.id')
                )
            )
        ));

        $this->Lot->contain(array('Sale', 'Catalogue', 'Withdrawal', 'Horse', 'Vendor'));

我的分页器调用如下所示:

$this->paginate = array(
                    'contain' => array('Sale', 'Catalogue', 'Withdrawal', 'Horse', 'Vendor'),
                    'conditions' => array('Vendor.name' => 'Somerset Stud'),
                    'maxLimit' => ($this->params['ext'] == 'csv' ? 1000 : 25),
                    'limit' => ($this->params['ext'] == 'csv' ? 1000 : 25),
                );

那是我收到列 Vendor.name 不存在的错误。 如果有人能分享一些技巧和想法,我将不胜感激。

亲切的问候, 西蒙

【问题讨论】:

  • Simon,绑定后需要$this->Lot->contain(array('Sale', 'Catalogue', 'Withdrawal', 'Horse', 'Vendor'));吗?
  • @Moz Morris - 通常我不需要这个,尤其是当我可以在 Paginate 类中使用 contains 属性时。但是我在我的 AppModel 类中将 Recursive 设置为 -1,所以我需要重新声明所有关系,即使在上面动态创建它们时也是如此。
  • 当前你在哪个控制器
  • 我目前在LotsController工作

标签: model pagination cakephp-2.0


【解决方案1】:

据我所知,您不在供应商控制器/模型中,而是在其他模型中。

$this->paginate = array(
                  'contain' => array('Sale', 'Catalogue', 'Withdrawal','Horse',
                  'Vendor'  => array('conditions' => array('Vendor.name'=>'Stu'),
                                     'order'      => array(),
                                     'fields'     => array())
                );

上面的代码应该可以工作。可能有语法错误,但我希望你明白我的意思

这是http://book.cakephp.org/view/1323/Containable书中的一个例子

$this->Post->find('all', array('contain' => array(
    'Comment' => array(
        'conditions' => array('Comment.author =' => "Daniel"),
        'order' => 'Comment.created DESC'
    )
)));

【讨论】:

  • 感谢您的回复,但这会产生非常奇怪的结果。我使用了上面的语法,由 Vendor.name = 'HIGHLAND STUD' 过滤,结果是所有 349 个结果仍然呈现,但它们都具有相同的 HIGLAND STUD 供应商值,这是不正确的。总而言之,以上所做的就是为结果中的每条记录赋予相同的供应商价值。任何想法为什么?感谢您的努力。
  • 使用以下方法很奇怪:conditions => array('Horse.name' => 'someValue')... 而 Vendor.name 没有,它们都是我正在工作的控制器之外的控制器,并且都已更改为具有 hasOne 关系到地段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
相关资源
最近更新 更多