Query con Doctrine en Form

Versión 2 (Guillermo Zdanowicz, 14/06/2012 09:01)

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 2 Guillermo Zdanowicz
---
21 2 Guillermo Zdanowicz
para definir como devolver resultados se tiene
22 2 Guillermo Zdanowicz
---
23 2 Guillermo Zdanowicz
24 2 Guillermo Zdanowicz
$q = Doctrine_Query::create()
25 2 Guillermo Zdanowicz
   ->from('Post p')
26 2 Guillermo Zdanowicz
   ->setHydrationMode(Doctrine::HYDRATE_ARRAY);
27 2 Guillermo Zdanowicz
28 2 Guillermo Zdanowicz
29 2 Guillermo Zdanowicz
$resultSet = $q->execute(); // $resultSet is an array
30 2 Guillermo Zdanowicz
31 2 Guillermo Zdanowicz
32 2 Guillermo Zdanowicz
foreach ($resultSet as $post) {
33 2 Guillermo Zdanowicz
    // $post is an array
34 2 Guillermo Zdanowicz
    echo $post['title'];
35 2 Guillermo Zdanowicz
}
36 2 Guillermo Zdanowicz
37 2 Guillermo Zdanowicz
---
38 2 Guillermo Zdanowicz
39 2 Guillermo Zdanowicz
$q = Doctrine_Query::create()
40 2 Guillermo Zdanowicz
   ->from('Post p')
41 2 Guillermo Zdanowicz
   ->setHydrationMode(Doctrine::HYDRATE_RECORD); // Unnecessary, HYDATE_RECORD is default method
42 2 Guillermo Zdanowicz
43 2 Guillermo Zdanowicz
44 2 Guillermo Zdanowicz
$resultSet = $q->execute(); // $resultSet is an Doctrine_Collection object
45 2 Guillermo Zdanowicz
46 2 Guillermo Zdanowicz
47 2 Guillermo Zdanowicz
foreach ($resultSet as $post) {
48 2 Guillermo Zdanowicz
    // $post is an Post object
49 2 Guillermo Zdanowicz
    echo $post->getTitle();
50 2 Guillermo Zdanowicz
    echo $post['title']; // Each Doctrine's Model object implements ArrayAccess interface so this is possible
51 2 Guillermo Zdanowicz
    echo $post->myCustomMethod();
52 2 Guillermo Zdanowicz
}
53 2 Guillermo Zdanowicz
---
54 2 Guillermo Zdanowicz
55 2 Guillermo Zdanowicz
$q = Doctrine_Query::create()
56 2 Guillermo Zdanowicz
   ->select('p.created_at')
57 2 Guillermo Zdanowicz
   ->from('Post p')
58 2 Guillermo Zdanowicz
   ->where('p.id = ?', 321)
59 2 Guillermo Zdanowicz
   ->setHydrationMode(Doctrine::HYDRATE_SINGULAR_SCALAR); 
60 2 Guillermo Zdanowicz
61 2 Guillermo Zdanowicz
62 2 Guillermo Zdanowicz
$createdAt = $q->execute(); 
Redmine Appliance - Powered by TurnKey Linux