CakePHP 1.3包含无法正确建立关联

编程入门 行业动态 更新时间:2024-10-24 10:23:16
本文介绍了CakePHP 1.3包含无法正确建立关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的第一个问题.我在cakephp 1.3中遇到了可控行为.问题如下:我有一个具有以下关联的可交付模型:

this is my first question here. I'm having an issue with containable behavior in cakephp 1.3. The issue is as follows: I have a Deliverable model with the following associations:

class Deliverable extends AppModel { var $name = 'Deliverable'; var $displayField = 'name'; var $actsAs = array('Containable'); var $belongsTo = array( 'Asset' => array( 'className' => 'Asset', 'foreignKey' => 'asset_id', 'dependent' => false ), 'DelRouteName' => array( 'className' => 'DelRouteName', 'foreignKey' => 'del_route_name_id', ), 'AccessGroup' => array( 'className' => 'AccessGroup', 'foreignKey' => false, 'conditions' => 'Deliverable.role_id = AccessGroup.id' ) ); var $hasOne = array( 'Artifact' => array( 'className' => 'Artifact', 'foreignKey' => 'deliverable_id', 'dependent' => true, ) ); var $hasMany = array( 'Delivery' => array( 'className' => 'Delivery', 'foreignKey' => 'deliverable_id', 'dependent' => true, 'order' => 'Delivery.gmt_created', ) );

这是分页设置的配置:

$this->paginate['Deliverable'] = array_merge($this->paginate['Deliverable'], array( 'limit' => $this->getSysPref('items_per_page', 20), 'conditions' => array( 'Deliverable.role_id' => $this->myRoleSetIds(), 'Deliverable.asset_id' => $asset['Asset']['id'], 'Delivery.gmt_created >' => $keep_time ), 'order' => 'Deliverable.name', 'contain' => array( 'AccessGroup', 'Delivery' => array( 'conditions' => array( 'Delivery.gmt_created >' => $keep_time ), 'limit' => 1, 'order' => 'Delivery.gmt_created DESC' ), 'DelRouteName' ) )); $deliverables = $this->paginate('Deliverable');

如您所见,附加行为,定义的关联以及所包含的关联模型(在分页配置部分中).

As you can see, the containable behavior is attached, the associations defined, and the associated models contained (in the pagination configure section).

我遇到的这个问题是,它能够成功查询所包含的模型"AccessGroup"和"DelRouteName",但无法获取关联的传递"数据.仅当我在针对子模型(交付)的父模型(交付)上设置条件时才会显示该错误,这是我之前做过的并且完全合法.我收到此SQL错误,并且您可以看到,表传递根本没有加入,但access_groups和del_route_names却加入了.

This issue I'm having is that it is able to successfully query the contained models "AccessGroup" and "DelRouteName", yet it doesn't fetch the associated "Delivery" data. The error only shows up if I set a condition on the parent model (Deliverable) targeting the child model (Delivery), which I've done before and is perfectly legal. I get this SQL error and as you can see the table deliveries is not joined at all, yet access_groups and del_route_names are.

这是蛋糕构建的SQL:

Here is the SQL that cake builds:

SELECT "Deliverable"."id" AS "Deliverable__id", "Deliverable"."name" AS "Deliverable__name", "Deliverable"."artifact_name" AS "Deliverable__artifact_name", "Deliverable"."type" AS "Deliverable__type", "Deliverable"."asset_id" AS "Deliverable__asset_id", "Deliverable"."del_route_name_id" AS "Deliverable__del_route_name_id", "Deliverable"."artifact_id" AS "Deliverable__artifact_id", "Deliverable"."status" AS "Deliverable__status", "Deliverable"."delivery_id" AS "Deliverable__delivery_id", "Deliverable"."deliverable_id" AS "Deliverable__deliverable_id", "Deliverable"."gmt_created" AS "Deliverable__gmt_created", "Deliverable"."locked" AS "Deliverable__locked", "Deliverable"."frozen" AS "Deliverable__frozen", "Deliverable"."cloaked" AS "Deliverable__cloaked", "Deliverable"."role_id" AS "Deliverable__role_id", "DelRouteName"."id" AS "DelRouteName__id", "DelRouteName"."name" AS "DelRouteName__name", "DelRouteName"."type" AS "DelRouteName__type", "AccessGroup"."id" AS "AccessGroup__id", "AccessGroup"."name" AS "AccessGroup__name", "AccessGroup"."dn" AS "AccessGroup__dn", "AccessGroup"."filters" AS "AccessGroup__filters", "AccessGroup"."paths" AS "AccessGroup__paths" FROM "deliverables" AS "Deliverable" LEFT JOIN "del_route_names" AS "DelRouteName" ON ("Deliverable"."del_route_name_id" = "DelRouteName"."id") LEFT JOIN "access_groups" AS "AccessGroup" ON ("Deliverable"."role_id" = "AccessGroup"."id") WHERE "Deliverable"."role_id" = ('0') AND "Deliverable"."asset_id" = '2' AND "Delivery"."gmt_created" > '1361391936' ORDER BY "Deliverable"."name" ASC LIMIT 50

这给了我这个错误(使用PostgreSQL)

which gives me this error (using postgresql)

Warning (2): pg_query() [php/function.pg-query]: Query failed: ERROR: missing FROM-clause entry for table "Delivery"

我在这里很茫然,所以任何帮助将不胜感激.让我知道您是否还需要更多信息.

I'm at a loss here, so any help would be greatly appreciated. Let me know if you need more information as well.

推荐答案

您不能从主模型中限制包含的模型.在执行contain()时,CakePHP对每个模型使用单独的查询.如果您需要添加跨模型条件,则需要使用加入.

You cannot condition contained models from your main model. CakePHP uses separate queries for each model when doing contain(). If you need to add conditions that are cross-model, you'll need to use JOINs.

更多推荐

CakePHP 1.3包含无法正确建立关联

本文发布于:2023-11-23 01:20:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   CakePHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!