如何只获取rightJoin()中的选定字段并在cakephp3

时间:2016-04-03 10:34:15

标签: cakephp cakephp-3.2

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 Propertyid AS Property__idPropertycompany_id AS   Property__company_idPropertyaddress1 AS Property__address1,   Propertypostcode AS Property__postcode,   Tenanciesproperty_id AS Tenancies__property_id,   Tenanciesstart_date AS Tenancies__start_date,   Tenanciesend_date AS Tenancies__end_date,   Tenanciesdeposit_total AS Tenancies__deposit_total FROM   property Property正确加入tenancy Tenancies开启   (Tenancies.property_id = Property.id和Tenancies.active = 1)WHERE   (Property.active = 1 AND Propertycompany_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
                ]
            ]
        ]
    ]
],

0 个答案:

没有答案