0

数据应仅显示包含数据的租约。在正常查询中,我通常这样做是正确的加入。

我知道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'
        ]);
4

2 回答 2

0

无法通过hasMany关联的收容措施

这不能通过容器来完成,因为容器hasMany是通过单独的查询来检索的。相反,您必须自己添加一个适当的连接,如果您想将结果限制为仅具有关联的结果,则INNER连接(比连接更便携)可以做到这一点。RIGHTTenancies

一个简单的Query::innerJoinWith()无条件,和一些分组以避免重复

$properties = $this
    ->find()
    // ...
    ->innerJoinWith('Tenancies')
    ->group('Property.id');

...你就完成了,这将添加一个像这样的连接

INNER JOIN tenancies Tenancies ON Property.id = Tenancies.property_id

innerJoinWith()也适用于所有其他类型的协会

然而,对于belongsTohasOne关联,它可以通过包含来完成,对于这些类型的关联,所有数据都在同一个查询中检索。要按具有关联记录的那些进行过滤,只需将连接类型更改为INNER,例如

$this
    ->find()
    ->contain([
        'BelongsToAssociated' => [
            'joinType' => 'INNER'
        ]
    ]);

也可以看看

于 2016-04-03T16:08:35.827 回答
0

当我简单地更改containsmatching.

似乎有可能,请参阅按关联数据过滤

于 2019-04-18T18:14:46.703 回答