Cphalcon: [BUG]: Model::find()->toArray() do not execute afterFetch

Created on 5 Feb 2020  路  8Comments  路  Source: phalcon/cphalcon

I don't know if it's a bug or it's designed to be it.

If i do this, the afterFetch event is not triggering.

$model = new Model()
$result = $model->find();
$result->toArray();

But on this way works

$model = new Model()
$result = $model->find();
$result->jsonSerialize();
bug not a bug

All 8 comments

That's actually correct.

toArray method called on resultset does not hydrate models. It returns array from resultset, it only uses custom map if exists.

On the other hand jsonSerialize in deed hydrate models, that's why afterFetch is called.

Not sure if we want to fix it. We maybe could add another method to Array with hydration.

Oh ok @Jurigag I thought so, could be, add another method, maybe, but on jsonSerialize some model i'm getting this error.

Observation using or not using afterFetch

{
    "class": "/var/www/html/tkl-api/app/modules/content/controllers/CategoryController.php",
    "line": 49,
    "message": "Trying to call method getshared on a non-object",
    "status": false,
    "code": 0,
    "stack": [
        {
            "function": "getModelsMetaData",
            "class": "Phalcon\\Mvc\\Model",
            "type": "->"
        },
        {
            "function": "toArray",
            "class": "Phalcon\\Mvc\\Model",
            "type": "->"
        },
        {
            "function": "jsonSerialize",
            "class": "Phalcon\\Mvc\\Model",
            "type": "->"
        },
        {
            "file": "/var/www/html/tkl-api/app/modules/content/controllers/CategoryController.php",
            "line": 49,
            "function": "jsonSerialize",
            "class": "Phalcon\\Mvc\\Model\\Resultset",
            "type": "->"
        },
        {
            "function": "listAction",
            "class": "App\\Modules\\Content\\Controllers\\CategoryController",
            "type": "->"
        },
        {
            "function": "callActionMethod",
            "class": "Phalcon\\Dispatcher\\AbstractDispatcher",
            "type": "->"
        },
        {
            "function": "dispatch",
            "class": "Phalcon\\Dispatcher\\AbstractDispatcher",
            "type": "->"
        },
        {
            "file": "/var/www/html/tkl-api/app/bootstrap.php",
            "line": 29,
            "function": "handle",
            "class": "Phalcon\\Mvc\\Application",
            "type": "->"
        },
        {
            "file": "/var/www/html/tkl-api/public/index.php",
            "line": 2,
            "args": [
                "/var/www/html/tkl-api/app/bootstrap.php"
            ],
            "function": "require"
        }
    ]
}

@Jurigag can you help me, any idea

It's a table view i have 2, one works jsonSerialize the another no, also if i do a foreach on the resultset i get the same error if i do a $row->toArray()

@Jurigag can you help me, any idea

It's a table view i have 2, one works jsonSerialize the another no, also if i do a foreach on the resultset i get the same error if i do a $row->toArray()

https://github.com/phalcon/cphalcon/issues/14814

https://github.com/phalcon/cphalcon/blob/master/phalcon/Mvc/Model.zep#L1761 you have error here. For some reason container property is not set for your model.

Container is set on construct of model:
https://github.com/phalcon/cphalcon/blob/master/phalcon/Mvc/Model.zep#L143

/**
         * We use a default DI if the user doesn't define one
         */
        if typeof container != "object" {
            let container = Di::getDefault();
        }

        if unlikely typeof container != "object" {
            throw new Exception(
                Exception::containerServiceNotFound(
                    "the services related to the ODM"
                )
            );
        }

        let this->container = container;

Check what Di::getDefault() returns.

Add method:

public function getModelsMetaData()
{
    return parent::getModelsMetaData();
}

And put breakpoint inside of her and check what container property is.

@tidytrax Had any luck checking the suggestion of @Jurigag ?

Sorry for the delay, the problem was because I had on some View's that contains a container column, so that was the problem so I rewrite the view's. I saw that has a issue open about it already, and probably will be a reserved prop. thanks. as toArray have a alternative possibility about execute afterFetch would be nice.

@tidytrax Closing this as not an issue. Feel free to reopen if I misunderstood your reply.

Was this page helpful?
0 / 5 - 0 ratings