Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Once the record is in memory, save changes error #503

Closed
dreamsxin opened this issue Mar 23, 2013 · 3 comments
Closed

Once the record is in memory, save changes error #503

dreamsxin opened this issue Mar 23, 2013 · 3 comments

Comments

@dreamsxin
Copy link
Contributor

code:
models:

<?php
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Contacts extends \Phalcon\Mvc\Model
{   
    public function validation()
    {
        $this->validate(new Uniqueness(array(
            'field' => array('account_id', 'phone'),
        )));

        return $this->validationHasFailed() != true;
    }

}
        $contact = Contacts::findFirst();
        if ($contact == FALSE) {
            $contact = new Contacts();
        }
        $contact->phone = '1001';

        if ($contact->update() == false) {
            foreach ($contact->getMessages() as $message) {
                echo $message;
            }
        }

display:
Value of field 'Array' is already present in another record

@dreamsxin
Copy link
Contributor Author

SQL Statement: UPDATE "contacts" SET "account_id" = ?, "phone" = ?, "fullname" = ?, "sex" = ?, "created" = ? WHERE "id" = ?

has no change fields should not to save.

@roman-kulish
Copy link

Try creating separate Uniqueness validator for each unique I think it cannot handle a set of fields. There is another bulletproof way to solve it - unique index for these fields in the database :-)

@phalcon
Copy link
Collaborator

phalcon commented Apr 9, 2013

Set a custom message in the validator:

<?php
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Contacts extends \Phalcon\Mvc\Model
{   
    public function validation()
    {
        $this->validate(new Uniqueness(array(
            'field' => array('account_id', 'phone'),
           'message' => 'Account_id and phone must be unique'
        )));

        return $this->validationHasFailed() != true;
    }

}

@phalcon phalcon closed this as completed Apr 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants