Yii Cjuiautocomplete For Multiple Values

Hello Everyone :)

I am a Yii Beginner and I am currently working on a Tagging system where I have 3 tables:

-Issue (id,content,publish_date,…etc)

-Tag (id,tag)

-Issue_tag_map (id,tag_id_fk,issue_id_fk)

In my /Views/Issue/_form I have added a MultiComplete Extension to retrieve multiple tag ids and labels,

I have used an afterSave function in order to directly store the Issue_id and the autocompleted Tag_ids in the Issue_tag_map table, where it is a HAS_MANY relation.

Unfortunately Nothing is being returned.

I wondered if there might be a way to store the autocompleted Tag_ids in a temporary attribute and then pass it to the model’s afterSave() function.

I have been searching for a while, and this has been driving me crazy because I feel I have missed a very simple step!

Any Help or advices of any kind are deeply appreciated! Thank You :)

MultiComplete in Views/Issue/_form:


  <?php


     echo $form->labelEx($model, 'Tag');

     $this->widget('application.extension.MultiComplete', array(

    'model' => $model,

    'attribute' => '', //Was thinking of creating a temporary here

    'name' => 'tag_autocomplete',

    'splitter' => ',',

    'sourceUrl' => $this->createUrl('Issue/tagAutoComplete'),

    // Controller/Action path for action we created in step 4.

    // additional javascript options for the autocomplete plugin

    'options' => array(

        'minLength' => '2',

    ),

    'htmlOptions' => array(

        'style' => 'height:20px;',

    ),

));

echo $form->error($model, 'attribute');

?>

AfterSave in /model/Issue :


  protected function afterSave() {

    parent::afterSave();


    $issue_id = Yii::app()->db->getLastInsertID();


    $tag; //here I would explode the attribute retrieved by the view form

    // an SQL with two placeholders ":issue_id" and ":tag_id"

    if (is_array($tag))

        foreach ($tag as $tag_id) {

            $sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";     


            $command = Yii::app()->db->createCommand($sql);

           // replace the placeholder ":issue_id" with the actual issue value


            $command->bindValue(":issue_id", $issue_id, PDO::PARAM_STR);

           // replace the placeholder ":tag_id" with the actual tag_id value


            $command->bindValue(":tag_id", $tag_id, PDO::PARAM_STR);

            $command->execute();

        }

}

And this is the Auto Complete sourceUrl in the Issue model for populating the tags:


 public static function tagAutoComplete($name = '') {


    $sql = 'SELECT id ,tag AS label FROM tag WHERE tag LIKE :tag';

    $name = $name . '%';

    return Yii::app()->db->createCommand($sql)->queryAll(true, array(':tag' => $name));

}

actionTagAutoComplete in /controllers/IssueController:


// This function will echo a JSON object 

// of this format:

// [{id:id, name: 'name'}]

function actionTagAutocomplete() {


    $term = trim($_GET['term']);

    if ($term != '') {

        $tags = issue::tagAutoComplete($term);

        echo CJSON::encode($tags);

        Yii::app()->end();

    }

}

The solution is very simple and direct, installing the activerecord-relation-behavior,

Creating a MANY_MANY relation ship with the issue_Tag_map table through the issue model then just adding this line to botht he CreateAction and the UpdateAction $mode->tags = $_Post[issue][tag] // $_Post[MODELNAME][TAGS_ARRAY_ATTRIBUTE]