Jump to content

How to work with the 'self :: HAS_MANY'?


Yuri79

Recommended Posts

Hi guys, I can not find documentation on the subject:
 
I look /classes/Product.php, and i can not understand how arrays are defined here:
 

class ProductCore extends ObjectModel
{
***
public static $definition = array(
'table' => 'product',
'primary' => 'id_product',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
 
*** SOME 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'),
),
);
 
***
}

 
I'm interested in the string:

  'categories' => array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),

Apparently, there is given a link from one product and a few categories.
 
I want to make an analogy in my module, but I can not figure out which fields that are responsible for:
 
 

 array('type' => self::HAS_MANY, 'field' => 'WHAT IS IT?', 'object' => 'WHAT IS IT?', 'association' => 'WHAT IS IT?')

 
 
Help me understand?

Thanks!!!

Link to comment
Share on other sites

this is Prestashops way of associating the Product to a Category

'categories' => array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),

'type' defines the relationship.  HAS_MANY means a single Product can have many categories, if it were HAS_ONE, it would mean a single Product can have 1 Category

 

'field' defines the foreign relationship key.  What attribute of the Category makes the relationship unique.  In this case, that would be id_category

 

'object' defines the class for which the relationship is to. In this case Product has a relationship with "Category"

  • Like 2
Link to comment
Share on other sites

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...