每個Artikel
只有一個Barcode
。由於幾個原因,我想拆分Artikel
模型和Barcode
模型。當我從得到artikel表中的內容時,它返回一個包含正確條形碼部分的數組。但是,當我嘗試查找條形碼時,數組的artikel部分是空的。CakePHP與不同表有關係
這就是我的意思是:
// $this->Artikel->findById(102);
array(
'Artikel' => array(
'id' => '102',
'name' => 'Spätburgunder Spätlese Barrique',
'erzeuger_id' => '679',
'volumen_id' => '44',
'artikelgruppe_id' => '17'
),
'Barcode' => array(
'id' => '1',
'artikel_id' => '102',
'barcode' => '123456'
)
)
// $this->Barcode->findByBarcode(123456);
array(
'Barcode' => array(
'id' => '1',
'artikelnummer' => 'DE51076',
'barcode' => '123456'
),
'Artikel' => array(
'artikelnummer' => null, // this is null
'name' => null, // this is null as well
'erzeuger_id' => null, // also null
'volumen_id' => null, // ……
'artikelgruppe_id' => null // null
)
)
任何想法,我做錯了什麼?
這些
// Barcode.php
public $hasOne = array(
'Artikel' => array(
'className' => 'Artikel',
'foreignKey' => 'artikel_id'
)
);
// Artikel.php
public $hasOne = array(
'Barcode' => array(
'className' => 'Barcode',
'foreignKey' => 'artikel_id'
)
);
我覺得你可以寫一個從條形碼到Artickel的belongsTo關係 – Ananth 2015-02-06 12:26:08