数据应仅显示包含数据的租约。在正常查询中,我通常这样做是正确的加入。
我知道contain()
默认情况下是左连接,但可以包含右连接吗?
我的查询:
$properties = $this->find()
->select([
'Property.id', 'Property.company_id', 'Property.address1', 'Property.postcode',
])
->contain([
'Tenancies' => function($q) {
return $q
->select([
'Tenancies.id','Tenancies.property_id','Tenancies.created',
'Tenancies.stage', 'Tenancies.landlord_offer_sent', 'Tenancies.offer_letter_contract_id',
])
->contain([
'Tenants' => function($q) {
return $q
->select([
'Tenants.id', 'Tenants.stage', 'Tenants.tenancy_id', 'Tenants.holding_fee',
])
->where([
'active = 1',
]);
}
])
->where([
'Tenancies.active = 1',
]);
}
])
->where(['Property.active = 1', $conditions])
->toArray();
例如,我需要读取为空的租约的节点,'tenancies' => [],
并且它应该只显示节点 1。
打印=>
(int) 0 => object(App\Model\Entity\Property) {
'id' => (int) 95,
'company_id' => (int) 3,
'address1' => '40 Arthur Street',
'postcode' => 'LE11 3AY',
'tenancies' => [],
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Property'
},
(int) 1 => object(App\Model\Entity\Property) {
'id' => (int) 102,
'company_id' => (int) 3,
'address1' => 'Grace Dieu Court',
'postcode' => 'LE11 4QH',
'tenancies' => [
(int) 0 => object(App\Model\Entity\Tenancy) {
'id' => (int) 16,
'property_id' => (int) 102,
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2015-05-08T09:30:41+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'stage' => (int) 6,
'landlord_offer_sent' => false,
'offer_letter_contract_id' => (int) 37,
'tenants' => [
(int) 0 => object(Cake\ORM\Entity) {
'id' => (int) 16,
'stage' => (int) 7,
'tenancy_id' => (int) 16,
'holding_fee' => (float) 50,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Tenants'
},
(int) 1 => object(Cake\ORM\Entity) {
...
...
...
我尝试过内部连接'joinType' => 'INNER'
但没有运气:
class TenancyTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tenancy');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Properties', [
'foreignKey' => 'property_id',
'className' => 'property',
'joinType' => 'INNER'
]);