Reputation: 95
I've been losing my mind for this for a while now. I don't use Yii, I've just inherited some legacy code and I'm trying to wrap my head around it. I'm adding a record into the database using active record.
The code is this:
$result = new Result();
$result->setIsNewRecord(true);
$result->SourceId = Sources::CIP;
$result->PartnerId = $partner->Id;
$result->Result = '<h1>'.$partner->Name.'</h1>'.$raport;
$result->ResultTypeId = empty($nasoale)?9:8;
$result->Enabled = 1;
$result->Date = date('Y-m-d H:i:s');
$result->CreationUserId = Yii::app()->params['sistemUser'];
$result->LastUpdateUserId = Yii::app()->params['sistemUser'];
$result->save();
This does not give an error. It even has an Id of the "inserted" query. Problem is, it's not in the database. Right after inserting this, I have another function that searches based on "PartnerId" and "SourceId" and it actually finds the record. I don't exactly know how, but it does.
I insert other stuff into the database the same way, in the same table and I did not have this problem before.
Any help on this will be greatly appreciated.
Upvotes: 3
Views: 3158
Reputation: 10350
sounds like you have multiple issues stemming from the same core problem.
This is most likely because a transaction was started, but a ROLLBACK
occurred (or a COMMIT
was not issued )
This can be somewhat confirmed, if the table has an AUTO_INCREMENT
field and there are missing rows.
That is because you are not allowed to reuse AUTO_INCREMENT values, meaning if you rollback or do no commit, you "lose" the value...
Read more here (specifically the first comment)
This is most likely due to the way isolation levels work.
If the secondary function is running in the same thread and is part of the same transaction, it will read the data.
If the secondary function is not running in the same thread, but the isolation level allows for dirty reads, it will read the data.
If you require more help, give a shout in the comments.
Upvotes: 3
Reputation: 1152
Usually u will need to do some of this steps
if ($model->save()) { // do someting }
else { // do something other. u will find erros list here $model->errors;
or something like this
if ($model->validate()) { $model->save(false); }
else { // do something other }
Difference is because $model->save() by default do validation first and if it pass than save. But in some cases u will wish first to do validation and than if everything is ok do save, so u can pass false to save to escape validation.
In your case, since u dont have CDbException, its not issue with saving in database, it is usually issue with your model rules. So I suppose it will be best to check model attributes against your model rules, or even better your model rules against database and see if everything is ok. U can also do this by printing $model->errors on page if save() return false.
I hope this will help.
Upvotes: 1