Thank you for your reply redguy, but where possible, i’d rather avoid using find(). I’m making the uuid with beforesave() handler, so I just want to fill my model properties and save(). Than, I want my model to check itself before saving and update the existing record. What do I do?
Sorry for long reply, I can’t make more than 3 posts per day. I’m creating my models from different places, so everytime I create it I need to check if record already exists. And if does exist, I need to update the existing record, instead of inserting the new one.
And I don’t want to duplicate uuid calculating and model filling code.
I could write a special function and use it to add/update my records, but I don’t like this way because it seems not to be neat enough So I wonder if Yii has built-in tools to do this task (like “unique” validator in rules() or something).
Sorry for being so obscure, English is not my native.
I just want a good tidy way to get my task done:
I want to fill model’s properties and just save(). Then I want to calculate uuid and do some other operations in BeforeSave() handler, and then I want my model to check if this uuid already exists, and if it does, update the record.
It seems to me that there’s no way to do it clean and neat, but let me know if I’m wrong, please.
The beforeSave() method isn’t the right place to do this. You should probably create your own method within your model class (or a behavior if you need to reuse it) and encapsulate the logic within that. Your action would end up looking something like this:
$model = new Transactions;
$model->attributes = $_POST['Transactions'];
// Flash message, redirect, etc.