1

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

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');

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

我遇到的这个问题是它能够成功查询包含的模型“AccessGroup”和“DelRouteName”,但它没有获取相关的“Delivery”数据。只有当我在父模型(Deliverable)上设置针对子模型(Delivery)的条件时,才会出现该错误,这是我之前做过的并且完全合法的。我收到此 SQL 错误,您可以看到表交付根本没有加入,但 access_groups 和 del_route_names 是。

这是 cake 构建的 SQL:

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)

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

我在这里不知所措,所以任何帮助将不胜感激。如果您还需要更多信息,请告诉我。

4

2 回答 2

0

您不能根据主模型调整包含的模型。CakePHP 在执行contain(). 如果您需要添加跨模型的条件,则需要使用JOINs

于 2013-03-23T04:28:44.203 回答
0

得益于 Dave 的帮助,更新后的代码现在可以运行。

$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
            ),
            'joins' => array(
                array(
                    'table' => 'deliveries',
                    'alias' => 'Delivery',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Deliverable.id = Delivery.deliverable_id'
                    )
                )
            ),
            'order' => 'Deliverable.name',
            'contain' => array(
                'AccessGroup',
                'Delivery' => array(
                    'conditions' => array(
                        'Delivery.gmt_created >' => $keep_time
                    ),
                    'limit' => 1,
                    'order' => 'Delivery.gmt_created DESC'                      
                ),
                'DelRouteName'
            )
        ));
于 2013-03-25T15:45:44.217 回答