Jump to content

Model definition "associations" field


gostbuster

Recommended Posts

Hi all, 

While I was diving in the source code, I was wondering what was the "associations" field in definition used for :

 

Example in the product class :

class ProductCore extends ObjectModel
{
.....

public static $definition = array(
		'table' => 'product',
		'primary' => 'id_product',
		'multilang' => true,
		'multilang_shop' => true,
		'fields' => array(
			// Classic fields
.......
		'associations' => array(
			'manufacturer' => 				array('type' => self::HAS_ONE),
			'supplier' => 					array('type' => self::HAS_ONE),
			'default_category' => 			array('type' => self::HAS_ONE, 'field' => 'id_category_default', 'object' => 'Category'),
			'tax_rules_group' => 			array('type' => self::HAS_ONE),
			'categories' =>					array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),
			'stock_availables' =>			array('type' => self::HAS_MANY, 'field' => 'id_stock_available', 'object' => 'StockAvailable', 'association' => 'stock_availables'),
		),
	);

        'associations' => array(

 

How can we use this ? Is it a shortcut to load associated data ?

 

For example, If I would like to load the entire informations about the product manufacturer, could I do something like this :

 

$product->getManufacturer();

or $product->loadManufacturer();

 

Obviously this doesn't work, so I may miss something that could simplify the developments instead of doing a new sql request.

 

Thanks in advance,

 

 

 

Link to comment
Share on other sites

Hi gostbuster,

 

as you may see, the 'associations' defines the associations with other tables.

For example,

            'manufacturer' =>                 array('type' => self::HAS_ONE),

 
Shows that the product has (at most) one manufacturer  (in the 'fields' definition that you left out, you can find if a field is required or not. Manufacturer is not required, so 'at most')
 
and:
            'categories' =>                    array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),
 
shows that a product can have a relationship (i.e. belong to/'hang under') many categories. The relation with the Category is defined in the category_product table
 
 
in fields are the fields of the object defined, so you could use:
 
$product->id_manufacturer
or 
$product->manufacturer_name
 
 
to get the full manufacturer object, you could do something like:
 
$product_manufacturer = new Manufacturer((int)$product->id_manufacturer);
 
 
Hope this helps a little,
pascal.
Link to comment
Share on other sites

Hi PascalVG,

 

Yes I got that, so there is no shortcut to get related object entities,

 

How could I do to get an associated or related entity like for example the product categories ?

 

Of course I guess that there is a getCategories in class function which might do the job, but is there something to do with the association definition ?

 

Regards,

Link to comment
Share on other sites

  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...