Hello,
I have try everything but I have always the error "Property "Category.title_en" is not defined" when I try to save or to the test for the form !
Everything is fine to show the info, but it’s no possible to used it in a form or save.
I try to understand why the MultilingualBehavior to to find fields (title_en) that I don’y have in my model in fact.
Can somebody help me ?
Thanks
View :
<?php
$languages= Language::model()->findAll();
foreach ($languages as $language) {
if($language->code === Yii::app()->params['defaultLanguage']) $suffix = '';
else $suffix = '_'.$language->code;
?>
<fieldset>
<legend><?php echo $language->name; ?></legend>
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title'.$suffix,array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'title'.$suffix); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content'.$suffix); ?>
<?php echo $form->error($model,'content'.$suffix); ?>
</div>
</fieldset>
<?php } ?>
Model :
<?php
/**
* This is the model class for table "category".
*
* The followings are the available columns in table 'category':
* @property string $id_category
* @property string $id_parent
* @property integer $active
* @property string $date_add
* @property string $date_upd
* @property string $position
* @property string $name
* @property string $description
* @property string $keywords
*/
class Category extends MyActiveRecord
{
public function behaviors() {
return array(
'ml' => array(
'class' => 'MultilingualBehavior',
'localizedAttributes' => array('name', 'description', 'keywords'), //attributes of the model to be translated
),
);
}
public function defaultScope()
{
return $this->ml->localizedCriteria();
}
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Category 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 'category';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
$rules= array(
array('id_parent, date_add, date_upd', 'required'),
array('active', 'numerical', 'integerOnly'=>true),
array('id_parent, position', 'length', 'max'=>10),
array('name', 'length', 'max'=>128),
array('keywords', 'length', 'max'=>255),
array('description', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, id_parent, active, date_add, date_upd, position, name, description, keywords', 'safe', 'on'=>'search'),
);
return $rules;
}
/**
* @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(
'getparent' => array(self::BELONGS_TO, 'Category', 'id_parent'),
'childs' => array(self::HAS_MANY, 'Category', 'id_parent', 'order' => 'position ASC'),
'CategoryLang' => array(self::HAS_MANY, 'categoryLang', 'category_id'),
'childsCount' => array(self::STAT, 'Category', 'id_parent'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id Category',
'id_parent' => 'Id Parent',
'active' => 'Active',
'date_add' => 'Date Add',
'date_upd' => 'Date Upd',
'position' => 'Position',
'name' => 'Name',
'description' => 'Description',
'keywords' => 'Keywords',
'childsCount' => 'Sub-Categories',
);
}
/**
* 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($id_parent)
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,false);
$criteria->compare('id_parent',$id_parent,false);
$criteria->compare('active',$this->active);
$criteria->compare('date_add',$this->date_add,true);
$criteria->compare('date_upd',$this->date_upd,true);
$criteria->compare('position',$this->position,false);
$criteria->compare('name',$this->name,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('keywords',$this->keywords,true);
$criteria->order = 'position ASC';
return new CActiveDataProvider($this, array(
'criteria'=>$this->ml->modifySearchCriteria($criteria),
'pagination' => array(
'pageSize' => 40,
),
'sort'=>array(
'defaultOrder'=>'position ASC',
)
));
}
public function getMaxPosition($id_parent)
{
return Yii::app()->db->createCommand()
->select('max(position) as maxposition')
->from('category')
->where('id_parent = ' . $id_parent)
->queryScalar();
}
}
Controller :
public function actionUpdate()
{
$id = $_GET['id'];
$category = $this->loadModel($id, true);
$category->date_upd = new CDbExpression('NOW()');
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='update-form')
{
echo CActiveForm::validate($category);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['Category']))
{
$category->attributes=$_POST['Category'];
// validate user input and redirect to the previous page if valid
if($category->validate()) {
$category->save();
$this->redirect(array('index','id_parent'=>$category->id_parent));
}
}
// display the login form
$this->render('update',array('model'=>$category));
}