Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Models Manager Hydrate Mode

$this->modelsManager->createBuilder() ->from('Projects') ->leftJoin('Members') ->getQuery() ->execute();

This gives me whole query structure but i need only nested array. Is there a way to do it without foreach loop like setHydration.

Thank you

$this->modelsManager->createBuilder() ->from('Projects') ->leftJoin('Members') ->getQuery() ->execute()->toArray(); //This?

I watch Phalcon 2.0.0 and I didnt found any methods like setHydration mod on QueryBuilder or execute() methods but It's posible in Criteria in Phalcon 2, but not a public method

No in Phalcon 1 http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Criteria.html http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_ResultsetInterface.html

For now Phalcon ORM is useless Item

It think you can use @max variant or user Phalcon\Db\Result\Pdo fetch ASSOC http://docs.phalconphp.com/en/latest/api/Phalcon_Db_Result_Pdo.html

Bad news for you :)



85.4k

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model/resultset.zep#L385

I watch Phalcon 2.0.0 and I didnt found any methods like setHydration mod on QueryBuilder or execute() methods but It's posible in Criteria in Phalcon 2, but not a public method

No in Phalcon 1 http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Criteria.html http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_ResultsetInterface.html

For now Phalcon ORM is useless Item

It think you can use @max variant or user Phalcon\Db\Result\Pdo fetch ASSOC http://docs.phalconphp.com/en/latest/api/Phalcon_Db_Result_Pdo.html

Bad news for you :)

I saw this

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model/resultset.zep#L385 This doesnt do anything, only int type.

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model/resultset.zep#L385

I watch Phalcon 2.0.0 and I didnt found any methods like setHydration mod on QueryBuilder or execute() methods but It's posible in Criteria in Phalcon 2, but not a public method

No in Phalcon 1 http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Criteria.html http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_ResultsetInterface.html

For now Phalcon ORM is useless Item

It think you can use @max variant or user Phalcon\Db\Result\Pdo fetch ASSOC http://docs.phalconphp.com/en/latest/api/Phalcon_Db_Result_Pdo.html

Bad news for you :)



85.4k

You said:

Bad news for you :)

I didnt found any methods like setHydration

I'm showing you where the setHydration is

Oh, Sorry, my wrong message I was going to said "no setHydration was founded in Phalcon 1.3 and in Phalcon 2.0 It cant be used as hydration method" Phalcon 1.3 https://github.com/phalcon/cphalcon/search?utf8=%E2%9C%93&q=setHydration

You said:

Bad news for you :)

I didnt found any methods like setHydration

I'm showing you where the setHydration is



7.3k

You can determined any columns in your query as like :


return $this->getDI()->getModelsManager()->createBuilder()
            ->columns([
                'replays.*',
                'author.*',
                'discussion.*'
                ])
            ->from([
                'replays'         => '\Forum\Apps\Models\Replays',
                ])
            ->leftjoin('\Forum\Apps\Models\Users', 'replays.user_id = author.id', 'author')
            ->leftjoin('\Forum\Apps\Models\Discussions', 'replays.discussion_id = discussion.id', 'discussion')
            ->where('replays.discussion_id = :id:', array('id' => $id))
            ->orderBy(["replays.created_at DESC"])
            ->getQuery()
            ->execute();

Yes but i want to nested arrays with the sub models data. This give me one directional data. I magage to fix the situation with lopping through parent model's foreach and add joined data on every iteration.

You can determined any columns in your query as like :


return $this->getDI()->getModelsManager()->createBuilder()
           ->columns([
               'replays.*',
               'author.*',
               'discussion.*'
               ])
           ->from([
               'replays'         => '\Forum\Apps\Models\Replays',
               ])
           ->leftjoin('\Forum\Apps\Models\Users', 'replays.user_id = author.id', 'author')
           ->leftjoin('\Forum\Apps\Models\Discussions', 'replays.discussion_id = discussion.id', 'discussion')
           ->where('replays.discussion_id = :id:', array('id' => $id))
           ->orderBy(["replays.created_at DESC"])
           ->getQuery()
           ->execute();


7.3k

Don't know....

Result of call this method is full iterability :

This code is used in Replays model as


public function getreplaysFromDiscussion($id) {

        return $this->getDI()->getModelsManager()->createBuilder()
            ->columns([
                'replays.*',
                'author.*',
                'discussion.*'
                ])
            ->from([
                'replays'         => '\Forum\Apps\Models\Replays',
                ])
            ->leftjoin('\Forum\Apps\Models\Users', 'replays.user_id = author.id', 'author')
            ->leftjoin('\Forum\Apps\Models\Discussions', 'replays.discussion_id = discussion.id', 'discussion')
            ->where('replays.discussion_id = :id:', array('id' => $id))
            ->orderBy(["replays.created_at DESC"])
            ->getQuery()
            ->execute();                
    }

in controller -

$r = new Replays();
        $res = $r->getreplaysFromDiscussion(5)->getFirst();

        foreach ($res as $key => $value) {
            var_dump($res->$key->toArray());

        }

result


array(6) {
  ["id"]=>
  string(1) "3"
  ["body"]=>
  string(16) "replay bodt test"
  ["discussion_id"]=>
  string(1) "5"
  ["user_id"]=>
  string(1) "1"
  ["created_at"]=>
  string(2) "31"
  ["updated_at"]=>
  string(2) "31"
}
array(4) {
  ["id"]=>
  string(1) "1"
  ["email"]=>
  string(14) "[email protected]"
  ["nikname"]=>
  string(5) "user1"
  ["password"]=>
  string(5) "12345"
}
array(7) {
  ["id"]=>
  string(1) "5"
  ["title"]=>
  string(31) "test discuss with topic control"
  ["topic"]=>
  string(36) "test discuss with topic control body"
  ["categoriesId"]=>
  string(1) "1"
  ["usersId"]=>
  string(1) "1"
  ["createdAt"]=>
  string(10) "1409414692"
  ["slug"]=>
  string(31) "test_discuss_with_topic_control"
}

I don't see obstacles anything.

That's solution no doubt. i'm new at Phalcon (phalcon is great nut:)) but in Eloquent ORM you can join hasMany models in reqursive array. For example;

<?php
    Project::find(5)->with('Members')->get() 
?>

gives,

 [
    "id" => 5,
    "project_name" => "bla bla",
    "Members" => [
        ["id" => 88, "name" => "John"],
        ["id" => 89, "name" => "Fatih"],
        ..
    ]
 ]


7.3k
Accepted
answer

Then you can use Phalcon ORM. You will get same result.

No obstacle :

$r = new Replays();
        var_dump($r->findFirst()->getDiscussion()->toArray());

result

array(7) {
  ["id"]=>
  string(1) "5"
  ["title"]=>
  string(31) "test discuss with topic control"
  ["topic"]=>
  string(36) "test discuss with topic control body"
  ["categoriesId"]=>
  string(1) "1"
  ["usersId"]=>
  string(1) "1"
  ["createdAt"]=>
  string(10) "1409414692"
  ["slug"]=>
  string(31) "test_discuss_with_topic_control"
}

Phalcon allows you to do all that Laravel can, and more...

I'm using Phalcon and Laravel in my business.

Laravel is weaker than Phalcon in functionality, I experienced this in practice. :)