2016-03-24 9 views
0

私は与えられたプロパティIDを持つユーザーが必要です。 Property.id = $idbelongsToはどのようにmutipleで動作しますか?cakephp 3.x

このクエリを作成します。

// 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の関連付けを得ることができますが、それはユーザーのためのbelongsToを取得するために来るとき、それはテナントユーザーに関連付けられていない私に語った:

$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 
     "; 

は、ここに私の団体です。どのようにアソシエーションが設定され、正しいのか。私は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

アソシエーションが正しいと思うからといって、これが真でなければならないというわけではありません。彼らは正しいかもしれない、そうでないかもしれない、ここの人々はそれらを見ることなく伝えることができません。プログラミングの世界では、言葉はあまり価値がありません。コードは重要なものなので、常に関連コードをすべて含めてください!また、デバッグの試行を共有してください(他の封じ込め/結合を削除しようとしましたか?テナント表を介してユーザーに参加/参加するのも失敗しますか?など)。最後に、あなたの正確なCakePHPのバージョンを必ずご記入ください。 – ndm

+0

ありがとうございます。確かに私は答えを更新するつもりです – Fury

+0

@ndm関連が更新されました – Fury

答えて

1

、次のような関係があります。

プロパティhasManyの賃貸hasManyのテナントbelongsToのユーザを プロパティbelongsToの会社

あなたがテーブルクラスにそれを設定していると仮定すると、あなたは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

関連する問題