如何从cakephp3中的rightJoin()和contain()中仅获取选定字段
cakephp 3.2.3x 我试图从右连接中显示自定义字段,该字段与另一个表包含(左连接),但它不起作用。如何包含自定义字段是可行的 注1:当我摆脱如何从cakephp3中的rightJoin()和contain()中仅获取选定字段,cakephp,cakephp-3.2,Cakephp,Cakephp 3.2,cakephp 3.2.3x 我试图从右连接中显示自定义字段,该字段与另一个表包含(左连接),但它不起作用。如何包含自定义字段是可行的 注1:当我摆脱contain()时,我的ORM会像我期望的那样工作 $properties = $this->find() ->select([ 'Property.id', 'Property.company_id', 'Property.address1', 'Property.postc
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是正确的,但不适用于查询
'选择Property
id
ASProperty\uu id
,Property
公司id
AS
Property\uuuu company\u id
,Property
address1
作为Property\uu address1
,
属性
邮政编码
作为属性uu邮政编码
,
租赁
物业_id
作为租赁_物业_id
,
租赁
开始日期
作为租赁(开始日期)
,
租赁
结束日期
作为租赁
结束日期,
租赁
押金总额
作为租赁(押金总额)
来自
属性
属性
右连接租赁
租赁
(tenacities.property_id=property.id和tenacities.active=1)其中
(Property.active=1和Property
公司id=:c0)“
查询应该只显示“租约”的3个字段,但它检索所有租约
字段
(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
]
]
]
]
],
@ndm的可能重复当我使用contain()时,ORM检索所有rightJoin()或innerJoinWith()字段。这不是
rightJoin/innerJoinWith
用法的问题,只是没有定义要选择的字段(正确)。对于具有多个/belongtomany
关联,您不能从相应的包含配置之外的任何位置为包含选择字段。有关如何定义字段的示例,请参见可能的副本。