Query con Doctrine en Form
Versión 3 (Guillermo Zdanowicz, 14/06/2012 09:02)
| 1 | 1 | Guillermo Zdanowicz | h1. Query con Doctrine en Form |
|---|---|---|---|
| 2 | 1 | Guillermo Zdanowicz | |
| 3 | 1 | Guillermo Zdanowicz | public function obtenerPlanillasAreaAsignadas($area) |
| 4 | 1 | Guillermo Zdanowicz | { |
| 5 | 1 | Guillermo Zdanowicz | |
| 6 | 1 | Guillermo Zdanowicz | $q = Doctrine_Query::create() |
| 7 | 1 | Guillermo Zdanowicz | ->from('Planillas p'); |
| 8 | 1 | Guillermo Zdanowicz | |
| 9 | 1 | Guillermo Zdanowicz | $planillas_asignadas = $q->createSubquery() |
| 10 | 1 | Guillermo Zdanowicz | ->select('pa.planillas_id') |
| 11 | 1 | Guillermo Zdanowicz | ->from('PlanillasAreas pa') |
| 12 | 1 | Guillermo Zdanowicz | ->where('pa.areas_id = ?'); |
| 13 | 1 | Guillermo Zdanowicz | |
| 14 | 1 | Guillermo Zdanowicz | $q->where('p.id IN (' . $planillas_asignadas->getDql() . ')'); |
| 15 | 1 | Guillermo Zdanowicz | |
| 16 | 1 | Guillermo Zdanowicz | return $q->execute(array($area)); |
| 17 | 1 | Guillermo Zdanowicz | |
| 18 | 1 | Guillermo Zdanowicz | } |
| 19 | 2 | Guillermo Zdanowicz | |
| 20 | 3 | Guillermo Zdanowicz | |
| 21 | 2 | Guillermo Zdanowicz | --- |
| 22 | 3 | Guillermo Zdanowicz | |
| 23 | 2 | Guillermo Zdanowicz | para definir como devolver resultados se tiene |
| 24 | 3 | Guillermo Zdanowicz | |
| 25 | 2 | Guillermo Zdanowicz | --- |
| 26 | 2 | Guillermo Zdanowicz | |
| 27 | 2 | Guillermo Zdanowicz | $q = Doctrine_Query::create() |
| 28 | 2 | Guillermo Zdanowicz | ->from('Post p') |
| 29 | 2 | Guillermo Zdanowicz | ->setHydrationMode(Doctrine::HYDRATE_ARRAY); |
| 30 | 2 | Guillermo Zdanowicz | |
| 31 | 2 | Guillermo Zdanowicz | |
| 32 | 2 | Guillermo Zdanowicz | $resultSet = $q->execute(); // $resultSet is an array |
| 33 | 2 | Guillermo Zdanowicz | |
| 34 | 2 | Guillermo Zdanowicz | |
| 35 | 2 | Guillermo Zdanowicz | foreach ($resultSet as $post) { |
| 36 | 2 | Guillermo Zdanowicz | // $post is an array |
| 37 | 2 | Guillermo Zdanowicz | echo $post['title']; |
| 38 | 2 | Guillermo Zdanowicz | } |
| 39 | 2 | Guillermo Zdanowicz | |
| 40 | 2 | Guillermo Zdanowicz | --- |
| 41 | 2 | Guillermo Zdanowicz | |
| 42 | 2 | Guillermo Zdanowicz | $q = Doctrine_Query::create() |
| 43 | 2 | Guillermo Zdanowicz | ->from('Post p') |
| 44 | 2 | Guillermo Zdanowicz | ->setHydrationMode(Doctrine::HYDRATE_RECORD); // Unnecessary, HYDATE_RECORD is default method |
| 45 | 2 | Guillermo Zdanowicz | |
| 46 | 2 | Guillermo Zdanowicz | |
| 47 | 2 | Guillermo Zdanowicz | $resultSet = $q->execute(); // $resultSet is an Doctrine_Collection object |
| 48 | 2 | Guillermo Zdanowicz | |
| 49 | 2 | Guillermo Zdanowicz | |
| 50 | 2 | Guillermo Zdanowicz | foreach ($resultSet as $post) { |
| 51 | 2 | Guillermo Zdanowicz | // $post is an Post object |
| 52 | 2 | Guillermo Zdanowicz | echo $post->getTitle(); |
| 53 | 2 | Guillermo Zdanowicz | echo $post['title']; // Each Doctrine's Model object implements ArrayAccess interface so this is possible |
| 54 | 2 | Guillermo Zdanowicz | echo $post->myCustomMethod(); |
| 55 | 2 | Guillermo Zdanowicz | } |
| 56 | 3 | Guillermo Zdanowicz | |
| 57 | 2 | Guillermo Zdanowicz | --- |
| 58 | 2 | Guillermo Zdanowicz | |
| 59 | 2 | Guillermo Zdanowicz | $q = Doctrine_Query::create() |
| 60 | 2 | Guillermo Zdanowicz | ->select('p.created_at') |
| 61 | 2 | Guillermo Zdanowicz | ->from('Post p') |
| 62 | 2 | Guillermo Zdanowicz | ->where('p.id = ?', 321) |
| 63 | 2 | Guillermo Zdanowicz | ->setHydrationMode(Doctrine::HYDRATE_SINGULAR_SCALAR); |
| 64 | 2 | Guillermo Zdanowicz | |
| 65 | 2 | Guillermo Zdanowicz | |
| 66 | 2 | Guillermo Zdanowicz | $createdAt = $q->execute(); |