【问题标题】:How to multiple join in cakephp?如何多次加入cakephp?
【发布时间】:2013-02-15 05:34:47
【问题描述】:

我想在我的查询中加入join,但我无法获得任何成功,我想从product 表中找出productname,如果product_id 存在于productplan 表中。

 Producttable     
 id name
  1   a
  2   b 

 productplantable
 id product_id
  1   1
  2   2

这是我的查询:

$p_products = $this->ProductPlan->find('all',   
                                       array('fields'=> 
                                         array('ProductPlan.product_id',
                                               'Product.name')
                                       )
                                    );
$this->set('p_products',$p_products);

【问题讨论】:

  • 您是否定义了各自模型中两个表之间的关系?
  • 我确定关系没有问题,产品有很多关系,产品计划属于与产品的关系
  • 明显关系已经定义
  • 尝试在您的查找查询上方添加$this->ProductPlan->recursive = 2
  • @Rikesh 不,伙计,它不起作用

标签: php mysql cakephp cakephp-1.3 cakephp-2.0


【解决方案1】:

如果您的关系在您的 ProductPlan 模型中设置正确,则以下内容应该有效。如果没有,请提供您的 ProductPlan 模型的源代码。

$this->ProductPlan->Behaviors->attach('Containable');

$p_products = $this->ProductPlan->find('all', array(
  'fields'  => array('product_id'),
  'contain' => array('Product' => array('fields' => array('name')))  
));
$this->set('p_products',$p_products);

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多