CakePHP 1.3 contain not building associations corr

2019-07-29 19:18发布

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条回答
神经病院院长
2楼-- · 2019-07-29 19:39

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.

查看更多
Bombasti
3楼-- · 2019-07-29 19:49

The updated code which now works thanks to the help of 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'
            )
        ));
查看更多
登录 后发表回答