How do I traverse CakePHP relations?

Fabio

I´m trying to traverse the relations on CakePHP models. This is the database: My database

I can access product attributes (product-> attributes) on a product model but I cannot access the product attributes on Ad model (ad->product->attributes).

Here is my code:

//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));

}

And here is what I do in the views:


//Products Views: snipet of view.ctp
print_r ($product);
// this prints rhe product and all associated attributes


//Ads Views: snipet of view.ctp
print_r ($ad['Product']);
//this will print only the product fields, but not the attributes associated to the product

What is wrong? How do I access the relation Ad->product->attribute from my Ad model ?

jsstrn

Perhaps the problem could be that you're not using CakePHP's conventions.

From the docs:

"new join table’s name needs to include the names of both models involved, in alphabetical order, and separated with an underscore ( _ )."

So, your join table should be named attributes_products.

Also, check your foreign keys. They should be in singular form.

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

Hopefully that solves the problem.

References:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I traverse CakePHP relations?

From Dev

How do I traverse an array diagonally in javascript

From Dev

How do I traverse to the next position of an array

From Dev

How to do recurrence relations?

From Dev

How to build proper CakePHP query for nested relations

From Dev

How to connect 3 tables in cakephp? (relations)

From Dev

NodeJS Loopback - How do I filter models by its relations

From Dev

How do I get an object and specific relations of that object in Parse?

From Dev

How do I create two relations in one document

From Dev

NodeJS Loopback - How do I filter models by its relations

From Dev

How do I upload a file within a database with 1 or more relations

From Dev

How do I filter a SQL query by a number of relations?

From Dev

How do I set default timezone in cakephp?

From Dev

Cakephp how do I overlap images

From Dev

How do I convert an integer to string in CakePHP?

From Dev

How do I traverse up out of shadow dom?

From Dev

NLTK: How do I traverse a noun phrase to return list of strings?

From Dev

How do I traverse a KDTree to find k nearest neighbors?

From Dev

How do I traverse through model relationships in laravel with dot syntax

From Dev

How do I traverse from child directories to root?

From Dev

What should I do? Doctrine relations

From Dev

Cakephp 3.0.3 How do I use the default layout?

From Dev

How do I build this complex database query in CakePHP 3.0?

From Dev

CakePHP - How do I implement blowfish hashing for passwords?

From Dev

How do I get a list of all functions inside a controller in cakephp

From Dev

How do I avoid html entities in the view in CakePHP?

From Dev

How do I get a random row in CakePHP 3.0?

From Dev

How do I best avoid inserting duplicate records in CakePHP?

From Dev

CakePHP and GoogleMapsHelper: How do I load markers from my DB?

Related Related

  1. 1

    How do I traverse CakePHP relations?

  2. 2

    How do I traverse an array diagonally in javascript

  3. 3

    How do I traverse to the next position of an array

  4. 4

    How to do recurrence relations?

  5. 5

    How to build proper CakePHP query for nested relations

  6. 6

    How to connect 3 tables in cakephp? (relations)

  7. 7

    NodeJS Loopback - How do I filter models by its relations

  8. 8

    How do I get an object and specific relations of that object in Parse?

  9. 9

    How do I create two relations in one document

  10. 10

    NodeJS Loopback - How do I filter models by its relations

  11. 11

    How do I upload a file within a database with 1 or more relations

  12. 12

    How do I filter a SQL query by a number of relations?

  13. 13

    How do I set default timezone in cakephp?

  14. 14

    Cakephp how do I overlap images

  15. 15

    How do I convert an integer to string in CakePHP?

  16. 16

    How do I traverse up out of shadow dom?

  17. 17

    NLTK: How do I traverse a noun phrase to return list of strings?

  18. 18

    How do I traverse a KDTree to find k nearest neighbors?

  19. 19

    How do I traverse through model relationships in laravel with dot syntax

  20. 20

    How do I traverse from child directories to root?

  21. 21

    What should I do? Doctrine relations

  22. 22

    Cakephp 3.0.3 How do I use the default layout?

  23. 23

    How do I build this complex database query in CakePHP 3.0?

  24. 24

    CakePHP - How do I implement blowfish hashing for passwords?

  25. 25

    How do I get a list of all functions inside a controller in cakephp

  26. 26

    How do I avoid html entities in the view in CakePHP?

  27. 27

    How do I get a random row in CakePHP 3.0?

  28. 28

    How do I best avoid inserting duplicate records in CakePHP?

  29. 29

    CakePHP and GoogleMapsHelper: How do I load markers from my DB?

HotTag

Archive