symfony2 - Can I set a better structure to Symfony 2 / doctrine results? -



symfony2 - Can I set a better structure to Symfony 2 / doctrine results? -

i'm building api in symfony , i'm struggling default format querybuilder gives me json.

in cakephp when info set nested array, each model separated out this

$this->person->find( 'first', array( 'conditions' => array( 'person.id' => $personid ), 'contain' => array( 'address' => array( 'fields' => array( 'address.town' ) ) ), 'fields' => array( 'person.id', 'person.name' ) ) );

gives me result this

array(2) [ 'person' => array(2) [ 'id' => string (3) "596" 'name' => string (13) "michael bates" ] 'address' => array(1) [ array(3) [ 'id' => string (3) "125" 'town' => string (8) "new york" 'addressesperson' => array(4) [ 'id' => string (4) "4270" 'address_id' => string (3) "125" 'person_id' => string (3) "596" ] ] ] ]

where person , address separated out.

in symfony/doctrine can't work out how same construction in result. when have this

$querybuilder = $this->createquerybuilder('person') ->select('person.id person_id, person.name') ->addselect('address.id address_id, address.town') ->leftjoin('person.address', 'address') ->where('person.id = :person_id') ->setparameter('person_id', $id); homecoming $querybuilder->getquery()->getresult();

i person , address info in same array this

array(1) { ["result"]=> array(1) { [0]=> array(4) { ["person_id"]=> string(3) "596" ["name"]=> string (13) "michael bates" ["address_id"]=> string(3) "125" ["town"]=> string (8) "new york" } } }

which pretty horrible complex result sets, , means have go through , give alias fields names clash (person.id , address.id above)

can models split in symfony / doctrine in way similar cakephp?

or have myself before homecoming result controller?

i don't think there way have 2 "splitted" result sets in single query in doctrine, why matter? doesn't sense right have 2 different entity results in single query.

what trying achieve?

it looks you're not using powerfulness of doctrine. seem have entities, why don't hydrate them?

$qb = $this->createquerybuilder('p') ->addselect('a') ->leftjoin('p.address', 'a') ->where('p.id', ':id') ->setparameter(':id', $id) ; homecoming $qb->getquery()->getresult();

this give array of person entities linked getaddress() address entity this

object (acme\foobundle\entity\person) { [id] => 596 [name] => "michael bates" [foo] => "bar" [address] => object (acme\foobundle\entity\address) { [id] => 125 [town] => "new york" [country] => "usa" } }

you'll have info need this. if want serialize (transform json), may want utilize jms/serializer (or bundle) handle hard work. @ controller level, this

$serializer = $this->get('jms_serializer.serializer'); $results = $this->getdoctrine()->getrepository('acmefoo:person')->findbyid($id); homecoming new response( $serializer->serialize($results, 'json'), 200, [ 'content-type' => 'application/json' ] );

which generate

[ { "id": 596, "name": "michael bates", "address": { "id": 125, "town": "new york" } } ]

if need array of address, may want create new method in addressrepository

public function findbypersonid($id) { $qb = $this->createquerybuilder('a') ->leftjoin('a.person', 'p') ->where('p.id', ':id') ->setparameter(':id', $id) ; homecoming $qb->getquery()->getresult(); }

which homecoming array of address matching person id. combine both accomplish trying @ first

$doctrine = $this->getdoctrine(); $persons = $doctrine->getrepository('acmefoo:person')->findbyid($id); $addresses = $doctrine->getrepository('acmefoo:address')->findbypersonid($id); $data = [ 'person' => $persons, 'address' => $addresses ];

however, take care of ignoring (in annotation), or removing bring together in person repository if utilize method

symfony2 cakephp doctrine2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -