Relationship between notes and tags I have 3 objects and 3 models. The note, have many tags, and every tags must have one note. So, I have a note_tag_list to store the tags and notes relationship. 

The program creates a note, with some tags. 

First, I create a note object, and put it into the model. And I get back the object from the model. I create the tag if it doesn't exist. Then, I read back the tag object from the DB. And use all the tags and note to create new note_tag_list object, and put this object to the model.

But I think it seems very complex for my flow. Any ways to do it more elegant? Thank you. 

//load all the object
$this-&gt;load-&gt;library('obj_note'); 
$this-&gt;load-&gt;library('obj_tag');  
$this-&gt;load-&gt;library('obj_note_tag_list'); 

//load all the model
$this-&gt;load-&gt;model('note_model');  
$this-&gt;load-&gt;model('tag_model');  
$this-&gt;load-&gt;model('note_tag_list_model'); 

//create a note object
$aNewUserNote    =new obj_note();

//set the object information
$user_id         =get_current_user_id();
$title           =$this-&gt;input-&gt;post('ui_title_input');
$content_url     =$this-&gt;input-&gt;post('ui_url_input');
$content         =$this-&gt;input-&gt;post('ui_textarea');
$right           =RIGHT_NOTE_PUBLIC;
$status          =STATUS_NOTE_ACTIVE;   

$aNewUserNote-&gt;set_user_id($user_id);
$aNewUserNote-&gt;set_title($title);
$aNewUserNote-&gt;set_content_url($content_url);
$aNewUserNote-&gt;set_content($content);
$aNewUserNote-&gt;set_right($right);
$aNewUserNote-&gt;set_status($status);

$aNewlyCreateNoteId = 0; 

if(!$this-&gt;note_model-&gt;create_note_by_note($aNewUserNote, $aNewlyCreateNoteId))
{          
    //create success
    return;
}   

//get back the object from the model
$aNewlyCreateObj = $this-&gt;note_model-&gt;read_note_by_note_id($aNewlyCreateNoteId);


$aTagNameString = $this-&gt;input-&gt;post('ui_tag');
$tagsStringArray = $this-&gt;convert_tags_string_to_lower_case_string_array($aTagNameString);

$aTagArray = array();  
$createNewTagIsSuccess = FALSE;

//Create New Tag
for($i = 0; $i &lt; count($tagsStringArray); $i++)
{   
    $aTagObj = $this-&gt;tag_model-&gt;read_tag_by_tag_name($tagsStringArray[$i]);
    if($aTagObj == NULL)
    {
        $aNewTag =new obj_tag();
        $aNewTag-&gt;set_create_user_id($user_id);
        $aNewTag-&gt;set_tag_name($tagsStringArray[$i]);

        if(!$this-&gt;tag_model-&gt;create_tag_by_tag($aNewTag)) //create tag fail
        {
          $createNewTagIsSuccess = FALSE;  
        }
    }
}  

//Read all the tag
for($j = 0; $j &lt; count($tagsStringArray); $j++)
{ 
    $aTagObj = $this-&gt;tag_model-&gt;read_tag_by_tag_name($tagsStringArray[$j]);
    $aTagArray[count($aTagArray)] = $aTagObj;
}                                            


//Make the note, and tag have relationship
for($k = 0; $k &lt; count($aTagArray); $k++)
{                                       
    $theTagObj = $aTagArray[$k];          


    $aNoteTagList = new obj_note_tag_list();
    $aNoteTagList-&gt;set_note_id($aNewlyCreateObj[0]-&gt;get_id());
    $aNoteTagList-&gt;set_tag_id($theTagObj[0]-&gt;get_id());

    $this-&gt;note_tag_list_model-&gt;create_note_tag_list_by_note_tag_list($aNoteTagList);
}
This:

//set the object information
$user_id         =get_current_user_id();
$title           =$this-&gt;input-&gt;post('ui_title_input');
$content_url     =$this-&gt;input-&gt;post('ui_url_input');
$content         =$this-&gt;input-&gt;post('ui_textarea');
$right           =RIGHT_NOTE_PUBLIC;
$status          =STATUS_NOTE_ACTIVE;   

$aNewUserNote-&gt;set_user_id($user_id);
$aNewUserNote-&gt;set_title($title);
$aNewUserNote-&gt;set_content_url($content_url);
$aNewUserNote-&gt;set_content($content);
$aNewUserNote-&gt;set_right($right);
$aNewUserNote-&gt;set_status($status);


Should be rewritten as this

// create and populate note object
$note = new obj_note();           
$note-&gt;set_user_id(get_current_user_id());
$note-&gt;set_title($this-&gt;input-&gt;post('ui_title_input'));
$note-&gt;set_content_url($this-&gt;input-&gt;post('ui_url_input'));
$note-&gt;set_content($this-&gt;input-&gt;post('ui_textarea'));
$note-&gt;set_right(RIGHT_NOTE_PUBLIC);
$note-&gt;set_status(STATUS_NOTE_ACTIVE);


Other than that, my comment on your question will explain the rest.