Я пытаюсь пересечь отношения на моделях CakePHP. Это база данные: Как мне пересекать отношения CakePHP?
я могу получить доступ атрибутов продукта (продукт-> атрибуты) на модели продукта, но я не могу получить доступ к атрибутам продукта на модели объявлений (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));
}
И вот что я в воззрениях:
// Продукты Просмотров: snipet из view.ctp
print_r ($ продукта);
// это печатает летящего продукта и все связанные с ним атрибуты
// Реклама Просмотров: snipet из view.ctp
print_r ($ объявления [ 'Product']);
// это напечатает только поля продукта, но не атрибуты, связанные с продуктом
Что не так? Как получить доступ к атрибуту Ad-> product-> из моей модели объявлений?
использование print_r ($ объявлений); вместо print_r ($ product); –