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

[BUG]: Model::findFirst() result miss toArray method and Hydration Modes #14534

Closed
bain2018 opened this issue Nov 15, 2019 · 3 comments
Closed
Labels
not a bug Reported issue is not a bug

Comments

@bain2018
Copy link

Describe the bug

$model=Model::findFirst();
$res=$model->toArray();
if i want to covert to array by toArray() ,i get an error:
Table '' doesn't exist in database when dumping meta-data for Model

doc https://docs.phalcon.io/4.0/en/db-models#hydration-modes

if i run this example
$model=Model::findFirst();
$model->setHydrateMode(
Resultset::HYDRATE_ARRAYS
);

the error is
The method 'setHydrateMode' doesn't exist on model

Details
Phalcon version:4.0 rc2
PHP Version: 7.3.11
Operating System: ubuntu

@niden
Copy link
Member

niden commented Nov 16, 2019

You need to check if your model is bound to the correct database table. Have a look at the initialize method of your model

class MyModel extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->setSource('database_table');
    }
}

The first error you get shows that you do not have a table defined there.

The findFirst returns back the model instance which indeed does not have the setHydrateMode in it. You can therefore use toArray() on it with optional field names passed (in the toArray) to get what you need.

If you have more results then you will get a resultset and setHydrateMode will return back arrays.

@niden niden added 4.0 not a bug Reported issue is not a bug and removed Bug - Unverified labels Nov 16, 2019
@bain2018
Copy link
Author

the reason refer to #14538

@bain2018
Copy link
Author

pls close this issue

@niden niden closed this as completed Nov 18, 2019
@niden niden added bug A bug report and removed bug A bug report labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug Reported issue is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants