2016-03-20 11 views
2

Ich habe eine Abfrage wie folgt, aber ich brauche nur bestimmte Felder nicht alle Felder. Ich habe die AutoFields auf "false" gesetzt, aber die Abfrage gibt immer noch alle Felder zurück.Wie bekomme ich nur ausgewählte Felder aus enthalten in CakePHP3

$tenancies = $this 
     ->find('all') 
     ->autoFields(false) 
     ->select([ 
      'Tenancy.id', 'Tenancy.created', 'Tenancy.stage', 
      'Properties.id', 'Properties.address1', 'Properties.postcode', 
      'Tenants.stage', 
     ]) 
     ->contain('Properties', function(\Cake\ORM\Query $query) { 
      return $query->where([ 
       'Properties.active' => 1 
      ]); 
     }) 
     ->contain(['Tenants']) 
     ->leftJoinWith('Tenants', function(\Cake\ORM\Query $query) { 
      return $query 
       ->autoFields(false) 
       ->select([ 
        'id', 'stage' 
       ]) 
       ->where([ 
        'Tenants.active' => 1 
       ]); 
     }) 
     ->where([ 
      'Tenancy.active' => 1, 
      $conditions 
     ]) 
     ->order([ 
      'Tenancy.created' => 'DESC', 
      'Tenants.tenancy_id' 
     ]) 
     ->group(['Properties.id']) 
     ->autoFields(false); 

    return $tenancies; 

Drucke =>

object(App\Model\Entity\Tenancy) { 

    'id' => (int) 3934, 
    'created' => object(Cake\I18n\FrozenTime) { 

     'time' => '2016-03-20T18:25:20+00:00', 
     'timezone' => 'UTC', 
     'fixedNowTime' => false 

    }, 
    'stage' => (int) 2, 
    'tenants' => [ 
     (int) 0 => object(Cake\ORM\Entity) { 

      'id' => (int) 8922, 
      'user_id' => (int) 56456, 
      'tenancy_id' => (int) 3934, 
      'needs_guarantor' => true, 
      'guarantor_id' => null, 
      'holding_fee' => (float) 0, 
      ... 
      ... 

Hinweis: Mit matching oder leftJoinWith Ich kann die Felder auswählen, die ich brauche, aber es funktioniert nicht auf contain

Antwort

4

Die wichtigste Antwort, die Sie ist müssen die Assoziationen korrekt eingestellt werden. Sobald die Verknüpfungen richtig eingestellt sind, können Sie es so einfach drucken.

return $this->find() 
       ->select([ 
        'Property.id', 'Property.company_id', 'Property.address1', 'Property.address2', 
        'Property.address3','Property.postcode', 
       ]) 
       ->contain([      
        'Tenancies' => function($q) { 
         return $q 
          ->select([ 
           'Tenancies.id','Tenancies.property_id','Tenancies.created', 
           'Tenancies.stage', 
          ]) 
          ->contain([ 
           'Tenants' => function($q) { 
            return $q 
             ->select([ 
              'Tenants.id', 'Tenants.stage', 'Tenants.tenancy_id', 'Tenants.holding_fee', 
             ]) 
             ->where([ 
              'active = 1', 
             ]); 
            } 
          ]) 
          ->contain([ 
           'Tenants' 
          ]) 
          ->where([ 
           'Tenancies.active = 1', 
          ]); 
        } 
       ]) 
       ->where(['Property.active = 1', $conditions]) 
       ->toArray(); 
+0

Sein sehr Gebrauch voll für mich. Ich danke dir sehr!! –

Verwandte Themen