problem saving relational tags

I’m working on a simple ‘photomanager’ with the ability to ‘tag’ photo’s.

In my form there is a textfield for comma separated tag-input.

For some reason some tags are saved twice or more in the database. See screen shot in attachment.

In my Foto model I do:




public function getRelatedTags()

        {

            $taglist=CHtml::listData($this->tags,'id','tag');

            return $taglist;

        }



In my Tag model:




public function normalizeTags($tags)

	{

	    $tags = explode(',', $tags);

	    foreach($tags as $tag)

	    {

		$tag = trim($tag);

		$tag = strtolower($tag);

		$tags[] = $tag;

	    }

	    return $tags;

	}



Foto Controller ActionCreate:




public function actionCreate()

	{

		$model = new Foto;

		$tag = new Tag;

                $relationFotoTag = new FotoTag;


		// Uncomment the following line if AJAX validation is needed

		//$this->performAjaxValidation($model);


		if(isset($_POST['Foto']))

		{

			$model->attributes=$_POST['Foto'];

			$model->image=CUploadedFile::getInstance($model,'image');

			if($model->save())

			{

				//sla origineel bestand op

				$model->image->saveAs(Yii::app()->basePath . '/../images/full/' . $model->id . '.jpg');

				$model->image = Yii::app()->basePath . '/../images/full/' . $model->id . '.jpg';

				

				//Sla preview bestand op

				$model->savePreview($model->image, Yii::app()->basePath . '/../images/preview/' . $model->id . '.jpg');

				

				//Sla thumb op

				$model->saveThumb($model->image, Yii::app()->basePath . '/../images/thumb/' . $model->id . '.jpg');

				

                                //sla de tags op

				$tag->attributes=$_POST['Tag'];

				$tags = $tag->normalizeTags($tag->tag);


				foreach($tags as $singletag)

				{

				    if(Tag::model()->findByAttributes(array('tag'=>$singletag)))

                                    {                                    

                                        $tag=Tag::model()->findByAttributes(array('tag'=>$singletag));                                        

                                    }

                                    else

                                    {

                                        $tag->isNewRecord = true;

                                        $tag->primaryKey = NULL;

                                        $tag->tag = $singletag;

                                    }

                                    $tag->save(false);

                                    //sla de relatie op in db

                                    $relationFotoTag->isNewRecord = true;

                                    $relationFotoTag->foto_id = $model->id;

                                    $relationFotoTag->tag_id = $tag->id;

                                    $relationFotoTag->save();

				}

                                                        

                                $this->redirect(array('view','id'=>$model->id));

			}

		}


		$this->render('create',array(

			'model'=>$model,

			'tag'=>$tag,

		));

	}



I have been working on this problem for quite some time by now, so any help would be appreciated.

Regards,

Sander

I don’t know for sure, but I would suggest you try this (IIRC the array you used would be initial values for a CDbCriteria attached to the class instance of Tag)




...

  if(Tag::model()->findByAttributes('tag=:singleTag', array(':singleTag'=>$singletag)))

...     



/Tommy

Issue may be you are adding normalized values to the same array.Just change array inside foreach to another name.Try with below coding


function normalizeTags($tags)

{

    $tags = explode(',', $tags);

    foreach($tags as $tag)

    {

        $tag = trim($tag);

        $tag = strtolower($tag);

        $tags_1[] = $tag;

    }

    return $tags_1;

}

Thanks a lot, sometimes it’s not that hard :wink: