cakephp 3.2.3x
我正在尝试显示来自右连接的自定义字段,该字段包含(左连接)与另一个表,但它无效。如何包含自定义字段正在运行。
注1:当我摆脱contain()
时,我的ORM按照我的预期运作。
$properties = $this->find()
->select([
'Property.id', 'Property.company_id', 'Property.address1', 'Property.postcode',
'Tenancies.property_id', 'Tenancies.start_date', 'Tenancies.end_date', 'Tenancies.deposit_total',
])
->rightJoin(['Tenancies' => 'tenancy'],[
'Tenancies.property_id = Property.id',
'Tenancies.active = 1'
])
->contain([
'Tenancies.Tenants' => function($q) {
return $q
->select([
'Tenants.id', 'Tenants.stage', 'Tenants.tenancy_id', 'Tenants.holding_fee',
])
->where([
'active = 1',
]);
}
])
->where(['Property.active = 1', $conditions]);
注2:生成的sql是正确的,但它不适用于查询。
'SELECT
Property
。id
ASProperty__id
,Property
。company_id
ASProperty__company_id
,Property
。address1
ASProperty__address1
,Property
。postcode
ASProperty__postcode
,Tenancies
。property_id
ASTenancies__property_id
,Tenancies
。start_date
ASTenancies__start_date
,Tenancies
。end_date
ASTenancies__end_date
,Tenancies
。deposit_total
ASTenancies__deposit_total
FROMproperty
Property
正确加入tenancy
Tenancies
开启 (Tenancies.property_id = Property.id和Tenancies.active = 1)WHERE (Property.active = 1 ANDProperty
。company_id
=:c0)'
查询应该只向我显示'Tenancies'的3个字段,但它会检索所有Tenancies
字段。
(int) 0 => [
'id' => (int) 102,
'company_id' => (int) 3,
'address1' => 'Grace Dieu Court',
'postcode' => 'LE11 4QH',
'tenancies' => [
(int) 0 => [
'id' => (int) 16,
'property_id' => (int) 102,
'landlord_id' => (int) 65,
'agent_id' => (int) 7,
'company_id' => (int) 3,
'bedroom' => (int) -1,
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2015-05-08T09:30:41+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'active' => true,
...
... ## } The rest of all fileds
'tenants' => [
(int) 0 => [
'id' => (int) 16,
'stage' => (int) 7,
'tenancy_id' => (int) 16,
'holding_fee' => (float) 50
],
(int) 1 => [
'id' => (int) 17,
'stage' => (int) 7,
'tenancy_id' => (int) 16,
'holding_fee' => (float) 50
]
]
]
]
],