【问题标题】:CakePHP : find data on multi modelCakePHP:在多模型上查找数据
【发布时间】:2014-04-25 12:54:38
【问题描述】:

我有一个表 products 有几个关联 示例:

我想在查看所有带有条件的产品的数据时使用

这是产品型号:

<?php
  class Product extends Model {
    public $hasAndBelongsToMany = array('Label','PircingScale');
    public $hasMany = array('LabelR','PircingScaleR');
  }

还有标签模型:

<?php
  class Label extends Model {
    public $hasAndBelongsToMany = array('Product');
   }

以及关系模型:

<?php
    class LabelR extends Model {
       public $useTable = 'labels_products';
       public $belongsTo = array('Product','Label');
    }

与 pircing_scale 模型相同

现在我在索引控制器上找到了:

<?php
 class IndexController extends AppController {
      public $uses = array('Product','Label','PircingScale');

      public function index() {      
         $this->set('lastNewProduct', $this->Product->LabelR->find('all', array(
           'conditions' => array('Label.id' => 1),
           'limit' => 3,
         )));
      }
}

在我看来,我想在 pircing_scale 中显示所有产品的信息

【问题讨论】:

  • 请告诉我们你到目前为止做了什么
  • 这个简短的问题有很多答案。请说明您的问题。
  • 对不起,我已经编辑了我的答案
  • 不,我想按标签查找所有产品,并使用此结果显示有关该产品的所有信息(例如,定价比例)

标签: cakephp model


【解决方案1】:

使用可包含的

Containable behavior 是一种核心行为,旨在让您定义查找结果的范围。

要使用它,只需将其添加到您要查询的模型中,或者添加到 AppModel 中即可与任何模型一起使用。

那么您的 find 调用将如下所示:

$this->Product->LabelR->find('all', array(
    'contain' => array(
        'Label',
        'Product' => array(
            'PircingScale'
        )
    ),
    'conditions' => array('Label.id' => 1),
    'limit' => 3,
 ));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多