gostbuster Posted September 28, 2014 Share Posted September 28, 2014 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 More sharing options...
PascalVG Posted September 28, 2014 Share Posted September 28, 2014 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 More sharing options...
gostbuster Posted September 28, 2014 Author Share Posted September 28, 2014 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 More sharing options...
oneyoney123 Posted November 30, 2014 Share Posted November 30, 2014 (edited) OK. So, we still need to load association object by ourself? It's not like rails or laravel, right? Edited November 30, 2014 by oneyoney123 (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now