|
Dec '16 |
12 |
2441 |
0 |
Someone correct me if I'm wrong, but there is no difference. Create() is calling save() at the end... :)
Documentation: https://docs.phalcon.io/en/3.0.0/reference/models.html#creating-updating-records
create()
will try to INSERT your data, while save()
will check if it already exists (by primary key), and will INSERT it if not and UPDATE it if it does.
The third relevant method is update()
, respectively
Will save() check for Pairwise uniqueness validator and then try to save it.
I'm not 100% positive, but according to the source code yes, I think it will traverse and check the added validators. https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model.zep#L1873
@Jurigag might have some useful insight ;]
Yea it will always check uniqueness validator.
If model state is dirty it will ignore current primary key of model(so if we have $model = Model::findFirst()
it won't check uniqueness against primarykey of $model
).
I want to check pair-wise validations for the data...then save the data...if validation is failed i want to continue(skip that data) .....How to do this?
i was running a for loop for reading the data in exl and saving the data....so when the duplicate element comes(when validation is failed ) i want to run this....
for(.....)
{
if(validation fails)
{
continue;
}
esle{
$this->save();
}
}
Skipping invalid data is implicit to save/create/update
foreach($this->request->getPost('data') as $item) {
$model = new MyModel();
$model->assign($item);
if(!$model->create()) {
// error
foreach($model->getMessages() as $msg) {
echo $msg->getMessage(), ' (', $msg->getType(), ' / ', $msg->getField(), ')', PHP_EOL;
}
} else {
// success
}
}
can we use save() to check records on the basis of other columns(not primary key) .
Actually.....according to my need when pair wise uniqueness validation is failed i need not give any error message or stop the process....but if any other valudations are failed (like Presence of) or any problem with db connections i need to display the error message and redirect to other controller/action. But all these comes under( $record->save()===False )case.
So how to skip uniqueness validation and give messages for other cases.
Modify my previous answer:
foreach($this->request->getPost('data') as $item) {
$model = new MyModel();
$model->assign($item);
if(!$model->create()) {
// error
foreach($model->getMessages() as $msg) {
if($msg->getType() != 'Uniqueness') {
// handle any error other than unique
$this->flashSession->error($msg->getMessage());
}
}
} else {
// success
}
}
I actually don't want to skip the uniqueness validator... I am reading data from excel which has department column in it for various corporates....so while saving the department record in department table ... i don't want save the duplicate records(only one pair of department-corporate ) can exist. So when the Pair wise validation is failed i will skip the current loop and go for the next data...
Can you tell a better way to do this other than checking from the model every time you save the data?
You are not getting me..!! I have other validations to check for the same model....which should give me error messages and rollback everything i had saved till now from exl sheet.