We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

phalcon exception not working

Hi

with below code I giving error:

                $user = new \Modules\Shared\Models\Profile();
                $user->username         = $this->request->getPost('username');
                .
                .
                .
                if ($user->create() == false)
                {
                    // Failed
                    foreach ($user->getMessages() as $message) {
                        $message = $message;
                    }
                    $this->flash->error($message);
                }
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'emo' for key 'username'' in /home/mojir/www/apps/frontend/controllers/AuthController.php:281 Stack trace: #0 [internal function]: PDOStatement->execute() #1 [internal function]: Phalcon\Db\Adapter\Pdo->executePrepared(Object(PDOStatement), Array, Array) #2 [internal function]: Phalcon\Db\Adapter\Pdo->execute('INSERT INTO `ma...', Array, Array) #3 [internal function]: Phalcon\Db\Adapter->insert('match_users', Array, Array, Array) #4 [internal function]: Phalcon\Mvc\Model->_doLowInsert(Object(Phalcon\Mvc\Model\MetaData\Memory), Object(Phalcon\Db\Adapter\Pdo\Mysql), 'match_users', false) #5 [internal function]: Phalcon\Mvc\Model->save() #6 /home/mojir/www/apps/frontend/controllers/AuthController.php(281): Phalcon\Mvc\Model->create() #7 [internal function]: Modules\Frontend\Controllers\AuthController->registerAction() #8 [internal function]: Phalcon\Dispatcher->dispatch() #9 /home/mojir/www/publi in /home/mojir/www/apps/frontend/controllers/AuthController.php on line 281

and if ($user->create() == false) is in line 281 !

so I can not handle the error message anyway

Please read the error message

Integrity constraint violation: 1062 Duplicate entry 'emo' for key 'username'

This means that an entry already exists in the table with the username "emo". The username column must have a primary or unique index.

You need to see if there are any users already inserted with that username before performing the create.

edited Sep '14

I know that. I want to handle this error and show an error to user instead.

If you read the rest of my post I also state that:

You need to see if there are any users already inserted with that username before performing the create

You can do this by performing a find() https://docs.phalcon.io/pt/latest/reference/models.html#finding-records

edited Sep '14

anyway I want to handle the error

edited Sep '14

Hi,

I think everyone here is focussing on the Uniqueness aspect of the actual error, not what EleRam is asking for.

The error is caused by an exception being raised - probably within the create method. You would need to wrap the create part with a try/catch block. something like this:

try { if ($user->create() == false) { ... other code here } } catch( PDOException $e ) { print 'An error occurred - more message details here'; }

I hope that helps.

this is very close to what I asked.

my question is this:

when my code is:

if ($user->create() == false)
{
   echo 'an error accurred';
} else
{
   echo 'new record added';
}

then when an error accurred, this line: echo "an error accured"; must be run. is'nt this?

Hi,

I think everyone here is focussing on the Uniqueness aspect of the actual error, not what EleRam is asking for.

The error is caused by an exception being raised - probably within the create method. You would need to wrap the create part with a try/catch block. something like this:

  try
  {
     if ($user->create() == false)
     {
        ... other code here
     }
  }
  catch( PDOException $e )
  {
      print 'An error occurred - more message details here';
  }

I hope that helps.



98.9k

I know an exception is raised there, it's uncaught because it's directly generated by the database server (PDOException) and not by the ORM. To allow Phalcon to handle this situation in advance, you need to define a UniquenessValidator as I mentioned before, this way you'll get a 'false' as you're expecting and a message in $user->getMessages()