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

Catch Exception on Model

Hi,

How catch an exception on a find ?

I have this exception : Table "signup" doesn't exist in database when dumping meta-data for Models******\Signup

My database haven't the table "signup", it's normal, but i want to catch this exception.

I try this but not working :

try {
    $tokenValid = $this->signup->find(
        [
            "conditions" => "******* = ?1 AND ***** = ?2",
            "bind"       => [
                1 => 0,
                2 => $******
            ]
        ]
    );
} catch (Exception $e) {
    throw new Phalcon\Mvc\Dispatcher\Exception();
}

Where can i put a try/catch on the model ???

PS : Sorry i'm bad in english

First have a look at your models. What are their names and which model you need to use to get your data from.

Then all you will need is to add a use statement at the top of your file and call find() on the model itself. Let us assume that you want to find if the token is valid for a user. We will use the Users table:

use MyApp\Models\Users;

try {
    $tokenValid = Users::findFirst(
        [
            "conditions" => "******* = ?1 AND ***** = ?2",
            "bind"       => [
                1 => 0,
                2 => $******
            ]
        ]
    );

    if (false === $tokenValid) {
        echo 'bad token';
    }
} catch (Exception $e) {
    throw new Phalcon\Mvc\Dispatcher\Exception();
}

Note that I called findFirst but depending on your application you can call find also and then check the results.

When you are in a controller or a component that is injection aware (i.e. you can access the DI services), whenever you do this:

$this->signup->....

Phalcon tries to find a service called signup or a property defined in your project called signup



4.2k
edited Oct '19

In the construct i have this

public function __construct()
    {
        parent::__construct();
        $this->signup = new Signup();
    }

I need to use static method ? can use my variable ?

If Signup is a model the code above is correct but your find() needs the model name. There is no need to create a new object in the constructor of your class. All you need is:

MyModel::find($conditions)

and that will return back the results.



4.2k
edited Oct '19

Sorry i don't understand.

When my BDD is correct, the find work fine. And i initialize the model for the save. Now i just want to catch any exception, but it's not working actually.

try
{
  Signup::find();
  //OR
  $this->signup->find();
}
catch (Exception $e)
{
    //not working
}

I try your code, but not working, i don't understand where i need to catch the exception...

I cannot figure out the logic you have i.e. where the exception is thrown in your code initially and under what condition. The try/catch block is correct.

Try something like this:

try {
    $tokenValid = $this->signup->find(
        [
            "conditions" => "******* = ?1 AND ***** = ?2",
            "bind"       => [
                1 => 0,
                2 => $******
            ]
        ]
    );

    if (true !== $tokenValid) {
        throw new Exception("The token is not valid")
    }

} catch (Exception $e) {
    throw new Phalcon\Mvc\Dispatcher\Exception();
}

In order to catch the exception you need to throw it first.



4.2k

I understand this. But the "find" method raise an exception, and it's this exception that i want to catch.

I try to explain the context : We have deploy the application on a new server. We forgot to initialise the database, so the database hasn't table. This code raise an exception and for the future i want to catch it. But it isn't working.

I hope you understand.... (very bad english....)



39.3k
Accepted
answer
edited Oct '19

If your database is not initialized i.e. no tables exist then you will get a Phalcon\Mvc\Model\Exception back.

So in an empty database

Signup::find();

will throw an exception like this:

Phalcon\Mvc\Model\Exception: Table 'signup' doesn't exist in database when dumping meta-data for .....

If you empty your database and run the code, the exception will be thrown. The sample script is:

// Setup DI
// Setup Database
Signup::find()

If in your code this code:

} catch (Exception $e) {

refers to the Phalcon\Mvc\Model\Exception you will catch it. Alternatively you can change the code to the parent \Exception

} catch (\Exception $e) {


4.2k
edited Oct '19

Thanks.... I'm really stupid

But i have another question. Can i verify, before execute any request, if the table exist ?

No question is a stupid question. Glad we found the issue.