dseslyh6662605 2013-03-22 20:34
浏览 44
已采纳

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

And here is the configuration of the pagination settings:

$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).

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.

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 

which gives me this error (using postgresql)

Warning (2): pg_query() [http://php.net/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.

  • 写回答

2条回答 默认 最新

  • dongzao9044 2013-03-23 04:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制