Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

what the difference between save() and create() while saving data in db.

edited Nov '16

Someone correct me if I'm wrong, but there is no difference. Create() is calling save() at the end... :)

Documentation: https://docs.phalconphp.com/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.

edited Nov '16

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 ;]

edited Nov '16

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?

What you mean skip that data ?

edited Dec '16

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
    }
}

? Phalcon will already cancel save when validation save. So just $model->save() is pretty much enough.

can we use save() to check records on the basis of other columns(not primary key) .

What you mean ? There is Uniqueness validator.

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.



42.3k
Accepted
answer

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
    }
}
edited Dec '16

But i don't understand, why you want to skip uniqueness validation if you want to check records on the basis of other columns(not primary key)? There is except column if you need to skip this validator is some other column value = anything.

edited Dec '16

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?

edited Dec '16

So use uniquness validator.

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.

edited Dec '16

If validation fails model is not saved to database.... If you want to rollback everything then use transaction and database unique index.