Development

ApplyingCustomJoinsUsingHelper: sfPropelCustomJoinHelper.class.php

You must first sign up to be able to contribute.

ApplyingCustomJoinsUsingHelper: sfPropelCustomJoinHelper.class.php

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