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

How to get from Model::find array of Model result (not Resultset\Simple)?

When I use Model::findFirst method, I get the Model object. When I use Model::find method, I get Phalcon\Mvc\Model\Resultset\Simple object. Is it any correct way to convert Phalcon\Mvc\Model\Resultset\Simple to Model[] (array of model) I try to do like:

return self::find([...])->filter(function ($child) {return $child;});

but I'm not sure that it's good way.

This way:

$array = Model::find()->toArray();

Model::find()->toArray() returns array of array. But I need array of objects, 'couse some functionality tied with the type of object

class MyClass extends Model
{
// ...
}

var_dump(MyClass::find()->toArray());
/**
array(112) {
  [0]=>
  array(28) {...},
  [1]=>
  array(28) {...},
  e.t.c.
*/

var_dump(MyClass::find([])->filter(function ($child) {
            return $child;
        });
);
/**
array(112) {
  [0]=>
  object(Namespace\MyClass)#54 (28) {...},
  [1]=>
  object(Namespace\MyClass)#98 (28) {...},
  e.t.c.
*/

It's not the same result.

Then you can hydrate the resultset as an array:

use Phalcon\Mvc\Model\Resultset;

var_dump(MyClass::find(['hydration' => Resultset::HYDRATE_ARRAYS]));

https://docs.phalcon.io/en/latest/reference/models.html#hydration-modes

'hydration' => Resultset::HYDRATE_ARRAYS

I've got object(Phalcon\Mvc\Model\Resultset\Simple)#95 (13). Did I do something wrong?

The Simple Resultset is built to behave like an array. Can I ask why you want an array of objects rather than the Simple Resultset?

edited Jun '15

I've got some model witch emplements one interface and do like this:

foreach (self::$models as $model) { // here we have same interface
    $res[] = $model::getResult(); // getResult() return an array of interface
}
$this->response->setJsonContent(array_merge([], ...$res));

Simple Resultset can't do this or I can't use it correctly.

When you hydrated as an array like Andres suggested, did you make sure to include the use line?

When you hydrated as an array like Andres suggested, did you make sure to include the use line?

Yep. Simple test:

// 1-st variant
class Files extends Model
{
    public static function test()
    {
        return self::find(['hydration' => \Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS]);
    }
}

$this->response->setJsonContent(Files::test());

/**
result: {}
*/

// ------------------------------------

// 2-nd variant
class Files extends Model
{
    public static function test()
    {
        return self::find([])->filter(function ($child) {
            return $child;
        });
    }
}

$this->response->setJsonContent(Files::test());

/**
result:
[
    {
        "ip": "127.0.0.1",
        "md5": "mxbvbwhq",
        "extension": "pdf",
        e.t.c.
    },
    {
        "ip": "127.0.0.1",
        "md5": "uuhs6vt8",
        "extension": "jpg",
        e.t.c.
    },
    e.t.c.
]
*/

I found that if the array parameter does not contain the columns field, it will return an ResultSet of models.

// \App\Models\Post
$posts = Post::find(['offset' => 0, 'limit' => 20]);  

// \Phalcon\Mvc\Model\Row
$posts = Post::find(['columns' => 'id, title', 'offset' => 0, 'limit' => 20]);