2017-03-03 10 views
0

私は以下のテーブルを持っています:cars、car_types、car_methods。これと私の問題は関係です。車には1つのタイプと1つの方法があります。CakePHP 3テーブル関係

私の目標は、車と彼の添付ファイル "car type"と "car_method"を表示することです。

車表:

id - type_id - method_id 

Car_typesテーブル:

id - type 

Car_methodsテーブル:

id - method 

方法CA私は次のように試みたが、それは、関連するエラー与えないだろう

$this->Cars->CarTypes->find('all'); 

車モデル:

を、私は私のコントローラの内部で行うことができるようにnは、私は私のモデルテーブル内のようなものをこれを設定しました
class CarsTable extends Table { 
public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->setTable('cars'); 
    $this->setDisplayField('id'); 
    $this->setPrimaryKey('id'); 

    $this->hasOne('CarTypes', [ 
     'className' => 'Cars.CarTypes', 
     'foreignKey' => 'type_id', 
     'propertyName' => 'type' 
    ]); 

    $this->hasOne('CarMethods', [ 
     'className' => 'Cars.CarMethods', 
     'foreignKey' => 'method_id', 
     'propertyName' => 'method' 
    ]); 
} 
} 

CarTypesモデル:

class CarTypesTable extends Table { 

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

    $this->setTable('car_types'); 
    $this->setDisplayField('id'); 
    $this->setPrimaryKey('id'); 

    $this->hasMany('Cars', [ 
     'foreignKey' => 'type_id' 
    ]); 
} 
} 

CarMethotsモデル:

class CarMethodsTable extends Table { 

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

    $this->setTable('car_method'); 
    $this->setDisplayField('id'); 
    $this->setPrimaryKey('id'); 

    $this->hasMany('Cars', [ 
     'foreignKey' => 'method_id' 
    ]); 
} 
} 
+0

を使用することができますか? https://book.cakephp.org/3.0/en/orm/associations.html – burzum

+0

はい、私はドキュメントを試してみましたが、私はそれをよく理解していません。私はあなたのためのコードを追加しました。 – CodeWhisperer

+0

それは種類と方法のために多くの車を持っています。今のように、テーブル自体に型とメソッドを指しています。この本の中のチュートリアルや、日付の関連付けに関する一般的なSQLチュートリアルを行ってください。:) – burzum

答えて

2

あなたの最後の二つの関係が間違っている、ここで良いものがある

CarTypesモデル:

public function initialize(array $config) { 
    $this->hasMany('Cars', [ //A CarType hasMany Cars 
     'foreignKey' => 'type_id' 
    ]); 
} 

CarMethodsモデル:

public function initialize(array $config) { 
    $this->hasMany('Cars', [ //A CarMethod hasMany Cars 
     'foreignKey' => 'method_id' 
    ]); 
} 

制御LERアクション

そして、あなたのクエリのために、含まれているオプションには、この2つの関係を追加します。

//Get all cars with type and method 
$this->Cars->find('all', 
    [ 
     'contain' => ['CarTypes', 'CarMethods'] 
    ] 
); 
+0

私の質問の中のモデルをあなたの例で更新しましたが、まだオブジェクトをエクスポートできません.CarsはCarTypeとCarMethodsに関連付けられていません}} – CodeWhisperer

+1

私はそれを解決しました。私のモデルの名前空間は、私のプラグインにリンクされていません。時には問題がとても簡単です。 :) 手伝ってくれてありがとう! – CodeWhisperer

0

The belongsTo association is a natural complement to the hasOne and hasMany associations: it allows us to see the data from the other direction.

belongsTo: the current model contains the foreign key.

あなたのテーブルの外部キーを持っている場合、その表は、関連するテーブルに属します。だから、あなたの場合には:この後

public function initialize(array $config) { 
    $this->belongsTo('CarTypes', [ 
     'foreignKey' => 'type_id', 
     'joinType' => 'LEFT' 
    ]); 

    $this->belongsTo('CarMethods', [ 
     'foreignKey' => 'method_id', 
     'joinType' => 'LEFT' 
    ]); 
} 

あなたがマニュアルをみました$this->Cars->CarTypes->find('all');

関連する問題