2013-12-23 46 views
1

我試圖遍歷CakePHP模型上的關係。 這是數據庫: My database如何遍歷CakePHP關係?

我可以訪問產品屬性(產品 - >屬性)上的產品型號,但我不能訪問AD模型(AD->產品 - >屬性)的產品屬性。

這裏是我的代碼:

//Product Model class Product extends AppModel { public $useTable = 'products'; public $displayField = 'name'; public $hasAndBelongsToMany = array( 'Attributes' => array( 'className' => 'Attribute', 'joinTable' => 'product_has_attributes', 'foreignKey' => 'products_id', 'associationForeignKey' => 'attributes_id', 'unique' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'with' => 'product_has_attributes' ) ); public $hasMany = array( 'Ads' => array( 'className' => 'Ad', 'foreignKey' => 'Products_id', 'conditions' => '', 'order' => '', 'limit' => '', 'dependent' => true ) ); //Ad Model class Ad extends AppModel { public $displayField = 'Name'; public $belongsTo = array( 'Product' => array( 'className' => 'Products', 'foreignKey' => 'products_id', 'conditions' => '', 'fields' => '', 'order' => '' ) ); //Attribute Model class Attribute extends AppModel { public $displayField = 'name'; public $hasAndBelongsToMany = array( 'Products' => array( 'className' => 'Product', 'joinTable' => 'product_has_attributes', 'foreignKey' => 'attributes_id', 'associationForeignKey' => 'products_id', 'unique' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'with' => 'product_has_attributes' ) );

// Products controller -> Action View class ProductsController extends AppController { public function view($id = null) { if (!$this->Product->exists($id)) { throw new NotFoundException(__('Invalid product')); } $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id)); $this->set('product', $this->Product->find('first', $options)); } }

// Ads controller -> Action View class AdsController extends AppController { public function view($id = null) { if (!$this->Ad->exists($id)) { throw new NotFoundException(__('Invalid ad')); } $options = array('conditions' => array('Ad.' . $this->Ad->primaryKey => $id)); $this->set('ad', $this->Ad->find('first', $options));

}

這裏是我做的觀點:


//產品瀏覽:view.ctp
的print_r的snipet( $產品);
//此打印RHE產品和所有相關的屬性


//廣告瀏覽次數:view.ctp
的print_r($廣告[ '產品'])的snipet;
//這將只打印產品字段,但不打印與產品關聯的屬性

什麼是錯的?如何從我的廣告模型訪問關係Ad-> product->屬性?

+0

使用的print_r($廣告);而不是print_r($ product); –

回答

1

也許問題可能是你沒有使用CakePHP的約定。

從文檔:

「新的連接表的名稱需要包括涉及,按字母順序排列兩種模型的名稱,並用下劃線(_)分離出來。」

所以,你的連接表應該被命名爲attributes_products

另外,檢查你的外鍵。它們應該是單數形式。

  • attributes_products.id
  • attributes_products.product_id
  • attributes_products.attribute_id

希望能解決這個問題。

參考文獻:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions

2

我可以想到幾個簡單的方法來實現這一點。

第一:

class AdsController extends AppController { 
    if (!$this->Ad->exists($id)) { 
     throw new NotFoundException(__('Invalid ad')); 
    } 
    $options = array(
     'conditions' => array('Ad.' . $this->Ad->primaryKey => $id), 
     'recursive' => 1, // or more 
    ); 
    $this->set('ad', $this->Ad->find('first', $options)); 
} 

這將是最簡單的代碼更改,以確保你得到的與返回的產品屬性。

否則,你在你的問題中提到了這一點。您可以通過模型訪問相關的車型,所以:

$this->Ad->Product->find(...); 

我曾與在使用條件相關的模型時,蛋糕不喜歡我找條件的問題,我不知道正確的語法,但如果你想要追求這一點,我相信你可以通過文檔或實驗來追蹤它。

最後,我建議你檢查ContainableBehavior,這將允許你調整結果中實際返回的字段。我希望這回答了你的問題!