custom relationships with more than one module

edited April 2016 in Modules
Hello!
There are three modules:
1. Products
2. Offers
3. Orders

All modules are related with each other in different tables, and I need ability to add and remove Products from Orders and Offers.
Products related to Offers and to Orders like this:
"Products_model"
public $belongs_to = array( 'offers' => array('model' => array('app' => 'offers_model'), 'relationships_model' => 'offers_relationship_model'), 'orders' => array('model' => array('app' => 'orders_model'), 'relationships_model' => 'orders_relationship_model'), );

Accordingly, Offers and Orders has many products:
"Offers_model"
public $has_many = array( 'products' => array('model' => array('app' => 'products_model'), 'relationships_model' => 'offers_relationship_model'), );
"Orders_model"
public $has_many = array( 'products' => array('model' => array('app' => 'products_model'), 'relationships_model' => 'orders_relationship_model') );
but there is a problem, if I add Product to Offer and to Order, it will be related only to Orders,
and the relationship between Product and Offers will be stored in the table 'orders_relationship', and it is wrong...
if in "Products_model" swap 'offers' and 'orders', like this:
"Products_model"
public $belongs_to = array( 'orders' => array('model' => array('app' => 'orders_model'), 'relationships_model' => 'orders_relationship_model'), 'offers' => array('model' => array('app' => 'offers_model'), 'relationships_model' => 'offers_relationship_model'), );
then the relationship will be maintained between the Product and Offer and the relationship between Product and Order will be stored in the table 'offers_relationship' (i.e backwards),
if in "Products_model" announce only one relationship with Orders or only with Offers - everything works as expected.

Prompt, please, what am I missing?

p.s.
custom relationships models are the same as "Fuel_relationships_model" only using another table in DB
FUEL_VERSION - 1.3.1

Comments

  • edited 6:08PM
    Tough to say what may be going on. I would recommend starting to debug in MY_Model::process_relationships() method which handles the saving of the relationships to see if there may be in issue in how it's saving.
  • edited 6:08PM
    Thanks for reply!
    I've checked method process_relationships() and did not find any issues:
    // then save foreach ($this->belongs_to as $related_field => $related_model) { if ( ! empty($this->normalized_save_data[$related_field])) { $fields = $rel_fields[$related_field]; $related_model = $related_models[$related_field]; // ------------------------------------------------------------------------ dump($related_model); dump($fields['relationships_model']); // ------------------------------------------------------------------------ // create relationships foreach ($this->normalized_save_data[$related_field] as $candidate_id) { // if it is an object, then we extract it's value if (is_object($candidate_id) AND method_exists($candidate_id, 'key_value')) { $candidate_id = $candidate_id->key_value(); } $CI->$relationships_model->save(array($fields['foreign_table'] => $this->table_name, $fields['foreign_key'] => $id, $fields['candidate_table'] => $CI->$related_model->table_name, $fields['candidate_key'] => $candidate_id)); } } }
    Models are fine:
    // RESULT $related_model = "offers_model"; $fields['relationships_model'] = "offers_relationship_model"; // RESULT $related_model = "orders_model"; $fields['relationships_model'] = "orders_relationship_model";

    I've also checked array that passed to save method $CI->$relationships_model->save() and got this result:
    Array ( [foreign_table] => products [foreign_key] => 407 [candidate_table] => offers [candidate_key] => 1 ) Array ( [foreign_table] => products [foreign_key] => 407 [candidate_table] => orders [candidate_key] => 1 )
    It seems that all is well to.

    If I use one at a time relationship or if I set same relationship model for both modules - it works fine,
    there is an issue only when there are more than one relationships model,
    and in this case related data saves to DB table which is declared in the last added relationships model...
    that is, if in my case there is:
    public $belongs_to = array( 'offers' => array('model' => 'offers_model', 'relationships_model' => 'offers_relationship_model'), 'orders' => array('model' => 'orders_model', 'relationships_model' => 'orders_relationship_model') );
    all related data will be stored in "orders_relationship" table which is used by "orders_relationship_model"

    any ideas about where can be that issue?
  • edited 6:08PM
    Got it!
    In MY_Model::process_relationships() method there is 2 conditions to handle has_many and belongs_to relationships,
    both are saving data with $CI->$relationships_model->save(),
    has_many is working fine and belongs_to has bug when there are more then one relationship model because it lacks one variable:
    // then save foreach ($this->belongs_to as $related_field => $related_model) { if ( ! empty($this->normalized_save_data[$related_field])) { $fields = $rel_fields[$related_field]; $related_model = $related_models[$related_field]; // create relationships foreach ($this->normalized_save_data[$related_field] as $candidate_id) { // if it is an object, then we extract it's value if (is_object($candidate_id) AND method_exists($candidate_id, 'key_value')) { $candidate_id = $candidate_id->key_value(); } $CI->$relationships_model->save(array($fields['foreign_table'] => $this->table_name, $fields['foreign_key'] => $id, $fields['candidate_table'] => $CI->$related_model->table_name, $fields['candidate_key'] => $candidate_id)); } } }
    and it should be like this:
    // then save foreach ($this->belongs_to as $related_field => $related_model) { if ( ! empty($this->normalized_save_data[$related_field])) { $fields = $rel_fields[$related_field]; $related_model = $related_models[$related_field]; // ------------------------------------------------------------------------ $relationships_model = $this->load_model($fields['relationships_model']); // ------------------------------------------------------------------------ // create relationships foreach ($this->normalized_save_data[$related_field] as $candidate_id) { // if it is an object, then we extract it's value if (is_object($candidate_id) AND method_exists($candidate_id, 'key_value')) { $candidate_id = $candidate_id->key_value(); } $CI->$relationships_model->save(array($fields['foreign_table'] => $this->table_name, $fields['foreign_key'] => $id, $fields['candidate_table'] => $CI->$related_model->table_name, $fields['candidate_key'] => $candidate_id)); } } }

    Thanks for the tip!
  • edited 6:08PM
    Thanks for reporting back. I'll add a fix to the develop branch.
Sign In or Register to comment.