You may be interested in what is suggested here
Take a look also to linked posts like this one
But, in any way, I have had same problem as well, and my desired workaround was one that offer a working solution with low code cost.
Of course thinking on that takes high time invest, but to have result is better than to just try things whose are not the expected.
In my case, a view gave the Read scenario some new attributes, whose had to be remove at Update or Insert ones (scenarios).
Problem was that validation process, when down to core classes (CActiveRecord / CModel) requires to have Read scenario available.
This means rewriting full validation methods, or dealing with two scenarios at once.
My solution is to retain Read scenario as long as possible, and then change the context when, in real, we are gonna write some data.
// At init time, it is supposed that read scenario reigns
public function init(){
$this->_tableName = 'view_name';
}
...
// Cause I found no other solution as well,
// this is my way for avoiding Yii _md private scope
protected function setMetaDataForWriting(){
$this->_tableName = 'table_name';
$this->getMetaData()->tableSchema->name = $this->_tableName;
$this->getMetaData()->tableSchema->rawName = $this->_tableName;
$columns = &$this->getMetaData()->tableSchema->columns;
$this->getMetaData()->tableSchema->columns = array_diff_key($columns,
array_flip(array('View_Added_Field1',
'View_Added_Field2',
..,
'View_Added_FieldN')));
}
// Ensure that a field for acting like primary key is present in your SQL View
public function primaryKey()
{
return 'id';
}
public function beforeSave(){
$this->setMetaDataForWriting();
return parent::beforeSave();
}
public function afterSave(){
$this->_tableName = 'view_name';
$this->refreshMetaData();
return parent::afterSave();
}