I want to insert tags separated by a comma into the database, but when it is inserted it strips off the last tag?
Controller
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$modelTags=$this->loadTagsModel($id);
$model->scenario = 'update';
$this->layout='update';
$this->picid=$model->id;
list($this->imgWidth, $this->imgHeight) = getimagesize('images/photos/originals/'.$model->id.'.jpg');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Photos'], $_POST['PhotosTags']))
{
$model->attributes=$_POST['Photos'];
$modelTags->attributes=$_POST['PhotosTags'];
$valid=$model->validate();
$valid=$modelTags->validate() && $valid;
if($valid){
if($model->status == 0){
$model->status = 1;
}
$modelTags->photo_id = $model->id;
$model->save();
if(!empty($modelTags->tags)){
$modelTags->save();
}
$this->redirect(array('index','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
'modelTags'=>$modelTags,
));
}
Model
<?php
/**
* This is the model class for table "photos_tags".
*
* The followings are the available columns in table 'photos_tags':
* @property integer $id
* @property integer $photo_id
* @property string $tags
*/
class PhotosTags extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return PhotosTags the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'photos_tags';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('tags', 'length', 'max'=>255),
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
array('tags', 'tagsLenght', 'lenght'=>3),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('tags', 'safe', 'on'=>'search'),
);
}
public function tagsLenght($attribute,$params)
{
$this->tags = substr($this->tags, 0, -1);
$tags = explode(",",$this->tags);
$tagsCount=count($tags);
if($tagsCount > $params['lenght']){
$this->addError($attribute, 'You can add a maximum of '.$params['lenght'].' tags');
}
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'photo_id' => 'Photo',
'tags' => 'Tags',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('photo_id',$this->photo_id);
$criteria->compare('tags',$this->tags,true);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
}