モデルのリレーションシップに関する本を読み、それに応じてモデルとデータベース テーブルを設定していれば、CakePHP を使用するとこれらすべてが非常に簡単になります。これは CakePHP の本当に重要な部分であり、アプリケーションの多くを構成するものです。これを「ケーキ」の方法で行う方法を理解すれば、生活がずっと楽になります。
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
MySQL の例として、このコードを試してください。明らかに、モデルの関係が正確にはわかりませんが、MySQL に基づく推測は次のとおりです。
// app/Model/Yarn.php
class Yarn extends AppModel {
public $belongsTo = array('YarnBrand');
// You might need this as $hasOne instead, I can't tell from the MySQL alone.
public $hasMany = array('Content');
}
// app/Model/YarnBrand.php
class YarnBrand extends AppModel {
public $hasMany = array('Yarn');
}
// app/Model/Content.php
class Content extends AppModel {
public $belongsTo = array('Yarn');
}
YarnBrand および Content とともに、すべての Yarn を検索するコード
$this->Yarns->find('all', array(
'conditions' => array(
Content.material_id' => 2
),
'contain' => array(
'YarnBrand',
'Content'
)
));