Development

ApplyingCustomJoinsUsingHelper: sfPropelCustomJoinHelper.class.3.php

You must first sign up to be able to contribute.

ApplyingCustomJoinsUsingHelper: sfPropelCustomJoinHelper.class.3.php

File sfPropelCustomJoinHelper.class.3.php, 4.9 kB (added by parkingmeter, 9 years ago)

Fix bug so that quiets errors when foreign keys may be null.

Line 
1 <?php
2
3 class sfPropelCustomJoinHelper
4 {
5   protected
6   $mainClassName = '',
7   $selectClasses,
8   $classOwnership;
9
10   public function __construct($mainClassName)
11   {
12     $this->mainClassName = $mainClassName;
13     $this->selectClasses = array();
14     $this->classOwnership = array();
15   }
16
17   public function addSelectTables()
18   {
19     $classes = array();
20     if (func_num_args() == 1) {
21       $classes = (array)func_get_arg(0);
22     } else {
23       $classes = func_get_args();
24     }
25     $this->selectClasses = array_merge($this->selectClasses, $classes);
26   }
27
28   public function clearSelectClasses()
29   {
30     $this->selectClasses = array();
31   }
32
33   public function hydrate($rs, $startCol=1)
34   {
35     $obj = new sfPropelCustomJoinObjectProxy($this->mainClassName);
36     $startCol = $obj->hydrate($rs, 1);
37
38     $childObjs = array();
39
40     // Initialize selected objects as children
41     foreach ($this->selectClasses as $childClassName)
42     {
43       $childObj = new sfPropelCustomJoinObjectProxy($childClassName);
44       $startCol = $childObj->hydrate($rs, $startCol);
45       if ($childObj->isAllPrimaryKeyNull()) {
46         $childObjs[$childClassName] = null;
47       } else {
48         $childObjs[$childClassName] = $childObj;
49       }
50     }
51
52     // Initialize child object relationships
53     foreach ($childObjs as $childClassName => $childObj)
54     {
55       $obj->addExternalObject($childObj, $childClassName); //main class object always holds all child objects.
56       if ($childObj != null && isset($this->classOwnership[$childClassName])) {
57         foreach ($this->classOwnership[$childClassName] as $subChild) {
58           if (array_key_exists($subChild[0], $childObjs)) {
59             $childObj->addExternalObject($childObjs[$subChild[0]], $subChild[1]);
60           }
61         }
62       }
63     }
64
65     return $obj;
66   }
67
68   public function doCount($c, $con=null)
69   {
70     return call_user_func_array(array($this->mainClassName.'Peer', 'doCount'), array($c, $con));
71   }
72
73   public function doSelect($c, $con=null)
74   {
75     $rs = $this->doSelectRS($c, $con=null);
76
77     $a = array();
78     while ($rs->next()) {
79       $a[] = $this->hydrate($rs);
80     }
81     return $a;
82   }
83
84   public function doSelectOne(Criteria $criteria, $con = null)
85   {
86     $critcopy = clone $criteria;
87     $critcopy->setLimit(1);
88     $objects = $this->doSelect($critcopy, $con);
89     if ($objects) {
90       return $objects[0];
91     }
92     return null;
93   }
94
95
96   /**
97    * Return the Propel ResultSet object.
98    *
99    * This method adds all columns specified with addSelectTables method.
100    * @see addSelectTables
101    */
102   public function doSelectRS($criteria, $con=null)
103   {
104     $c = clone $criteria;
105     $c->clearSelectColumns();
106
107     call_user_func(array($this->mainClassName.'Peer', 'addSelectColumns'), $c);
108     foreach ($this->selectClasses as $className) {
109       call_user_func(array($className.'Peer', 'addSelectColumns'), $c);
110     }
111     $rs = call_user_func_array(array($this->mainClassName.'Peer', 'doSelectRS'), array($c, $con));
112     return $rs;
113   }
114
115   public function setHas($className, $has, $alias = false)
116   {
117     if (!isset($this->classOwnership[$className])) {
118       $this->classOwnership[$className] = array();
119     }
120     $this->classOwnership[$className][] = array($has, $alias);
121   }
122
123   /**
124    * Do proxy call to peer method of the main class.
125    */
126   protected function __call($name, $args)
127   {
128     return call_user_func_array(array($this->mainClassName.'Peer', $name), $args);
129   }
130
131   /**
132    * Return the main class name. This is to work around when the pager object
133    * tries to include the Peer class file.
134    */
135   public function __toString()
136   {
137     return $this->mainClassName;
138   }
139 }
140
141 class sfPropelCustomJoinObjectProxy
142 {
143   protected
144   $className,
145   $obj,
146   $extObjs;
147
148   public function __construct($className)
149   {
150     $this->className = $className;
151     $this->obj = new $className;
152     $this->extObjs = array();
153   }
154
155   public function getDataObject()
156   {
157     return $this->obj;
158   }
159
160   public function isAllPrimaryKeyNull()
161   {
162     $pk = null;
163     $pks = $this->obj->getPrimaryKey();
164     foreach ((array)$pks as $pk) {
165     } if ($pk) {
166       return false;
167     }
168     return true;
169   }
170
171   public function __call($name, $args)
172   {
173     if (preg_match('/^get(.*)$/', $name, $matches))
174     {
175       if (array_key_exists($matches[1], $this->extObjs)) {
176         return $this->extObjs[$matches[1]];
177       }
178     }
179     return call_user_func_array(array($this->obj, $name), $args);
180   }
181
182   public function hydrate(ResultSet $rs, $startcol = 1)
183   {
184     $startcol = $this->obj->hydrate($rs, $startcol);
185     return $startcol;
186   }
187
188   public function addExternalObject($obj, $alias = false)
189   {
190     $key = $alias ? $alias: get_class($obj->getDataObject());
191     $this->extObjs[$key] = $obj;
192   }
193
194   public function __toString()
195   {
196     return $this->obj->__toString();
197   }
198 }
199