2016-03-24 105 views
0

我需要具有給定屬性ID的用戶,例如Property.id = $idbelongsTo如何與多個hasMany一起使用包含cakephp 3.x

我想構建此查詢。

$sql = "SELECT p.address1, p.address2, p.address3, p.postcode, 
       u.email, u.fullname 
       c.company_name, 
       tc.created, 
       tn.active 
     FROM property AS p 
     LEFT JOIN tenancy AS tc ON tc.property_id = p.id 
     LEFT JOIN tenant AS tn ON tn.tenancy_id = tc.id 
     LEFT JOIN users AS u ON u.id = tn.user_id 
     WHERE p.id = $id 
      AND p.active = 1 
      AND tc.active = 1 
      AND tn.active = 1 
      AND u.active = 1 
     "; 

這是我協會:

// Tenants 
class TenantTable extends Table 
{ 
public function initialize(array $config) 
{ 
    $this->table('tenant'); 
    $this->displayField('id'); 
    $this->primaryKey('id'); 
    $this->addBehavior('Timestamp'); 
    $this->belongsTo('Users', [ 
     'foreignKey' => 'user_id', 
     'joinType' => 'INNER' 
    ]); 
    $this->belongsTo('Tenancies', [ 
     'foreignKey' => 'tenancy_id', 
     'className' => 'tenancy', 
     'joinType' => 'INNER' 
    ]); 


// Users 
class UsersTable extends Table 
{ 

public function initialize(array $config) 
{ 
    $this->table('users'); 
    $this->displayField('name'); 

    $this->hasMany('Tenants', [ 
     'foreignKey' => 'user_id', 
     'foreignKey' => 'tenants' 
    ]); 
} 


// Tenancies 
class TenancyTable extends Table 
{ 

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('tenancy'); 
    $this->displayField('id'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsTo('Properies', [ 
     'foreignKey' => 'property_id', 
     'className' => 'property' 
    ]); 

    $this->belongsTo('Company', [ 
     'foreignKey' => 'company_id' 
    ]); 

    $this->hasMany('Tenant', [ 
     'foreignKey' => 'tenancy_id', 
     'className' => 'tenant' 
    ]); 


// Properties 
class PropertyTable extends Table 
{ 

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('property'); 
    $this->displayField('id'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsTo('Company', [ 
     'foreignKey' => 'company_id' 
    ]); 

    $this->hasMany('Tenancies', [ 
     'foreignKey' => 'property_id', 
     'className' => 'tenancy' 
    ]); 
} 

我能得到的hasMany關聯,但是當涉及到拿到屬於關聯的用戶來說,它告訴我,租戶不與用戶相關聯。協會如何設置,他們是否正確。我從PropertyTable運行以下ORM。

$query = $this->find() 
      ->select([ 
       'Property.id', 'Property.company_id', 'Property.address1', 'Property.address2', 'Property.address3','Property.postcode', 
       'Tenancies.id', 'Tenancies.property_id', 'Tenancies.created', 'Tenancies.stage', 'Tenancies.landlord_offer_sent', 
       'Company.id', 'Company.company_name', 
       'Tenants.id', 'Tenants.user_id', 'Tenants.stage', 
       'Users.id', 'Users.email', 'Users.fullname' 
      ]) 
      ->where(['Property.id' => $id]) 
      ->contain(['Company', 'Tenancies']) 
      ->leftJoinWith('Tenancies', function(\Cake\ORM\Query $query) { 
       return $query->where([ 
        'Tenancies.active' => 1, 
       ]); 
      }) 
      ->contain(['Tenancies.Tenants']) 
      ->leftJoinWith('Tenancies.Tenants', function(\Cake\ORM\Query $query) { 
       return $query->where([ 
        'Tenants.active' => 1, 
       ]); 
      }) 
/*======= The problem starts from here =========*/   
      ->contain(['Tenancies.Tenants.Users']) 
      ->leftJoinWith('Tenancies.Tenants.Users', function(\Cake\ORM\Query $query) { 
       return $query->where([ 
        'Users.active' => 1, 
       ]); 
      }); 

任何幫助pelase

+2

僅僅因爲你認爲這些關聯是正確的,那並不意味着這是真的。他們可能是正確的,也可能不是,這裏的人沒有看到他們就無法分辨。在編程世界中,單詞不值得太多,代碼是重要的,所以請儘快,_always_包含所有相關的代碼!也請分享您的調試嘗試(例如,您是否嘗試刪除其他遏制/連接?是否通過租戶表包含/加入用戶失敗?等等)。最後但並非最不重要的是,請始終提及您的確切CakePHP版本。 – ndm

+0

謝謝你的隊友。當然我會更新答案 – Fury

+0

@ndm關聯更新 – Fury

回答

1

那麼根據您向我們提供什麼,你有以下關係:

屬性的hasMany租賃的hasMany租戶屬於關聯用戶 屬性屬於關聯公司

假設你已經在你的表類中設置了,你可以使用co和co類似於你上面的做法:

public function getExample($id) { 
    return $this->find() 
    ->contain([ 
     'Company' => function ($q) { 
      return $q 
       ->select(['id', 'company_name']) 
       ->where(['Company.active = 1']); 
     }, 
     'Tenancies' => function($q) { 
      return $q 
       ->select(['Tenancies.id','Tenancies.property_id','Tenancies.created', 
        'Tenancies.stage','Tenancies.landlord_offer_sent', 
        'Tenants.id','Tenants.stage', 'Tenants.user_id', 'Tenants.tenancy_id', 
        'Users.id', 'Users.email', 'Users.fullname' 
       ])->leftJoin(['Tenants' => 'tenant'],[ 
        'Tenants.tenancy_id = Tenancies.id' 
       ])->leftJoin(['Users' => 'users'],[ 
        'Users.id = Tenants.user_id' 
       ]) 
       ->where([ 
        'Tenancies.active = 1', 
        'Tenants.active = 1', 
        'Users.active = 1', 
       ]); 
     } 
    ]) 
    ->where(['Property.active = 1', 'Property.id' => $id]) 
    ->toArray(); 
} 
+0

我編輯了這個,因爲連接類型有一個小錯誤。它現在應該與左連接:) – chrisShick

+0

謝謝你隊友你是明星。我在查詢上做了一些更新。它現在像charme一樣工作 – Fury

+0

要遵守CakePHP命名標準,您應該保留關係名稱的複數形式。只是一個建議:) – chrisShick

相關問題