has_many one to many

Hello i though we could have some one to many relationships
using "relationships_model"=>false
is that correct ?

public $has_many = array("videos"=>array('model'=>'videos_model'),"demos"=>array('model'=>'demos_model',"foreign_key"=>"realid","relationships_model"=>false)

all is fine
but when i save my record
it says An Error Was Encountered
Unable to locate the file: .php

I saw there might be a trouble with My_Model => function process_relationships($id)

maybe with a if (!empty($fields['relationships_model'])) {

should i fix this ? or am i wrong with my config ($has_many) ?

Comments

  • So here is the proposition to keep relationships_model=false possible


    public function process_relationships($id)
    {
    $CI =& get_instance();

    // handle has_many relationships
    if (!empty($this->has_many))
    {
    $rel_fields = $this->relationship_field_names('has_many');

    // first delete in case there are multiple saves to the same relationship table

    foreach ($this->has_many as $related_field => $related_model)
    {



    $clear_on_save = ((strtoupper($this->clear_related_on_save) == 'AUTO' AND isset($this->normalized_save_data['exists_'.$related_field])) OR $this->clear_related_on_save === TRUE);
    if ($clear_on_save)
    {
    $fields = $rel_fields[$related_field];

    if ($fields['relationships_model']==false || $fields['relationships_model']==FALSE ) {



    $CI->load->database();

    //$relModel= $this->load_related_model($related_model);
    $relModel = $CI->load->model($related_model['model']);

    $table=$relModel->table_name;
    //exit();

    // $query = $this->db->where('id', $rea->id)->get($table);
    $data = array(
    $related_model['foreign_key'] => 0
    );

    $this->db->where($related_model['foreign_key'], $id);
    $this->db->update($table, $data);

    }
    else {



    // remove pre-existing relationships
    if (!empty($fields['candidate_table']))
    {
    $del_where = array($fields['candidate_table'] => $this->table_name, $fields['candidate_key'] => $id);
    }
    else
    {
    $del_where = array($fields['candidate_key'] => $id);
    }

    $relationships_model = $this->load_model($fields['relationships_model']);

    $CI->$relationships_model->delete($del_where);

    }
    }

    }
    }



    // then save
    foreach ($this->has_many as $related_field => $related_model)
    {
    if ( ! empty($this->normalized_save_data[$related_field]))
    {
    $fields = $rel_fields[$related_field];

    if ($fields['relationships_model']!=false) {
    $related_model = $this->load_related_model($related_model);
    $relationships_model = $this->load_model($fields['relationships_model']);

    // create relationships
    foreach ($this->normalized_save_data[$related_field] as $foreign_id)
    {
    // if it is an object, then we extract it's value
    if (is_object($foreign_id) AND method_exists($foreign_id, 'key_value'))
    {
    $foreign_id = $foreign_id->key_value();
    }
    $CI->$relationships_model->save(array($fields['candidate_table'] => $this->table_name, $fields['candidate_key'] => $id, $fields['foreign_table'] => $CI->$related_model->table_name, $fields['foreign_key'] => $foreign_id));
    }
    }
    else {


    $fields = $rel_fields[$related_field];
    $related_model = $this->load_related_model($related_model);


    // create relationships
    foreach ($this->normalized_save_data[$related_field] as $foreign_id)
    {
    // if it is an object, then we extract it's value
    if (is_object($foreign_id) AND method_exists($foreign_id, 'key_value'))
    {
    $foreign_id = $foreign_id->key_value();
    }
    $me=$CI->$related_model->update(array($fields['foreign_key'] => $id),array("id"=>$foreign_id));
    }




    }
    }

    }
  • edited 2:32AM
    I'm sorry but I'm not quite sure I understand what you are proposing and what scenario you are using. Do you have a foreign key (e.g. my_foreign_table_id) that you want to map a relationship to. If so you, you can set the foreign_keys property on the model which will allow you to access the foreign object:

    EXAMPLE:

    Module Model Class
    class MY_module extends Base_module_model{ .... public $foreign_keys = array('other_key' => 'my_other_model'); ..... }

    Then you can access it like so:
    $record = fuel_model('my_module', 'key', 1); echo $record->other->id;
  • ok ! explanation (sorry) :

    I have a movie_model and a director_model

    in my movie_model i used $foreign_keys for director_model on director_id
    so i can select my director in the dropdown list in a movie record

    BUT i also want to have access to that relation in the director view
    so in that one i used a " has_many" config for the movies

    :)
  • the context is the admin view :)
  • edited June 2015
    That is the one relationship that will need to be done manually unfortunately (the opposite of the $foreign_keys relationship). Using a has_many actually saves to the fuel_relationships table and you'll want to save to the movie model. This will require you to add a multi field type to your directors form_fields method. Then in an on_after_save hook, you'll need to loop through the "movies" post values and save them to the movies table:
    public function on_after_save($values) { $values = parent::on_after_save($values); $posted = $this->normalize_save_values(); if (!empty($posted['movies'])) { $this->load->model('movies_model'); foreach($posted['movies'] as $key => $val) { $movie = $this->movies_model->find_by_key($val); $movie->director_id = $values['id']; $movie->save(); } } }
  • or actually my solution with relationships_model=false
    works perfectly with those little changes in the My_Model
    on one side on movie_model :
    public $foreign_keys = array('director_id' =>'director_model');
    and on the director_model
    public $has_many = array("movies"=>array('model'=>'movie_model',"foreign_key"=>"director_id","relationships_model"=>FALSE));

    it just works !

    so maybe this can help people

    or do you think this change in the My_Model can be dangerous ? :)

    thanks for all your answers
  • edited 2:32AM
    It's hard for me to see what changes you've made in the code above. Would it be possible to do a pull request on GitHub so I could see better what the changes are.
Sign In or Register to comment.