Is there any way to do this via updateAll() or updateByPk()? The reason I ask is because I have a similar situation but I want to use massive assignment, and I’m having problems with the $model->attributes() array always containing even columns that must not be changed.
my rules():
array('username, password, expire_date', 'required', 'on'=>'insert'),
array('username, expire_date', 'required', 'on'=>'update'),
array('user_id', 'numerical', 'integerOnly'=>true, 'on'=>'insert'),
array('username', 'length', 'max'=>45),
array('username', 'unique', 'allowEmpty'=>false, 'caseSensitive'=>false, 'on'=>'insert'),
array('password', 'length', 'max'=>255),
array('user_id, username, password, create_date, expire_date', 'safe', 'on'=>'search'),
my controller:
$model2= new UserLogins('update');
$model2->setScenario('update'); // Just making sure I'm on the "update" scenario
#$model2->unsetAttributes(); // Tried this one to delete all attributes, also tried "unset($model2->attributes) no luck
$model2->username = $_POST['UserLogins']['username'][$index];
if ( isset($_POST['UserLogins']['password'][$index]) && !empty($_POST['UserLogins']['password'][$index]) ){
// If password is set, change it, if not don't touch it.
$model2->password = crypt($_POST['UserLogins']['password'][$index], Randomness::blowfishSalt());
}else{
$model2->password = null;
}
$model2->expire_date = $_POST['UserLogins']['expire_date'][$index];
$model2->attributes = Array();
echo "<pre>model2->attributes is:<br>";
echo var_dump($model2->attributes);
echo "</pre>";
$model2->updateAll($model2->attributes,'user_login_id='.$currLoginId[0]['user_login_id']); //This gives me error about foreign key constraints
My var_dump() is always showing this:
model2->attributes is:
array(6) {
["username"]=>
string(7) "rrahim1"
["password"]=>
NULL
["expire_date"]=>
string(10) "2012-07-27"
["user_login_id"]=>
NULL
["user_id"]=>
NULL
["create_date"]=>
NULL
}
My goal is to have $model2->attributes that only has full values and no NULL fields at all. Why? Because my database complains if the updateAll() has columns that are used on other tables as foreign keys.
Can someone show me how I would achieve this?
Thank you in advance.