****************Model
#
#
#
#
Код: Выделить всё
public function rules() {
return array(
array('username,feedback_text', 'required', 'on' => 'update'),
array('address_house,address_street', 'required', 'on' => 'update'), //проверяем только при этом сценарии
............................
array('address_house,address_street',safe.....)
)
}
function lalala($_POST){
$user = new User() //!!!!!!! Новый пользователь
$user->setScenarion('tratata')
$user->scenarion = 'tratata'
$user->setAttributes($_POST); // В юзере все данные есть кроме 'address_house,address_street см rules
if($user->save()){ //всё ок валидация проходит модель сейвит
.....
}
...
// а вот здесь смотрим
if($user->validate()){ //мы получим False
ибо что ??????????? см ниже
}
}
и находим public function insert($attributes=null) <---- новая запись
Код: Выделить всё
public function insert($attributes=null)
{
if(!$this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
if($this->beforeSave())
{
Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$table=$this->getMetaData()->tableSchema;
$command=$builder->createInsertCommand($table,$this->getAttributes($attributes));
if($command->execute())
{
$primaryKey=$table->primaryKey;
if($table->sequenceName!==null)
{
if(is_string($primaryKey) && $this->$primaryKey===null)
$this->$primaryKey=$builder->getLastInsertID($table);
elseif(is_array($primaryKey))
{
foreach($primaryKey as $pk)
{
if($this->$pk===null)
{
$this->$pk=$builder->getLastInsertID($table);
break;
}
}
}
}
$this->_pk=$this->getPrimaryKey();
$this->afterSave();
$this->setIsNewRecord(false);
[b] $this->setScenario('update'); //<---------------wtf ???????????????
[/b] return true;
}
}
return false;
}
$this->setScenario('update'); //<---------------wtf ???????????????
Насколько это нормальное поведение менять сценарий после инсерта в модели?