root/trunk/patForms/Storage/Propel.php

Revision 343, 5.5 kB (checked in by sfuchs, 3 years ago)

Added a note to comments that addGenericAccessors/addGenericMutators have to be enabled for the Propel build

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * Storage for patForms integration with Propel
4  *
5  * Please note:
6  *
7  *        To use build-in patForms/Propel integration you have to build your
8  *        Propel classes with the following Propel build options enabled:
9  *
10  *         propel.addGenericAccessors = true
11  *         propel.addGenericMutators = true
12  *
13  *
14  * This class implements the patForms_Storage interface to integrate with
15  * a Propel object peer.
16  *
17  * Please note: this class is currently php5 only since its solely used
18  * as a base class for patForms_Definitions_Propel in order to integrate
19  * patForms with Propel5 as a "rapid form solution".
20  *
21  * $Id$
22  *
23  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
24  * @package        patForms
25  * @subpackage    Storage
26  * @license        LGPL
27  * @copyright    PHP Application Tools <http://www.php-tools.net>
28  * @see         patForms_Definition
29  */
30
31 /**
32  * Storage for patForms integration with Propel
33  *
34  * This class implements the patForms_Storage interface to integrate with
35  * a Propel object peer.
36  *
37  * Please note: this class is currently php5 only since its solely used
38  * as a base class for patForms_Definitions_Propel in order to integrate
39  * patForms with Propel5 as a "rapid form solution".
40  *
41  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
42  * @package        patForms
43  * @subpackage    Storage
44  * @license        LGPL
45  * @copyright    PHP Application Tools <http://www.php-tools.net>
46  * @see         patForms_Definition
47  */
48 class patForms_Storage_Propel extends patForms_Storage
49 {
50    /**
51     * Stores the classname of the Propel object
52     *
53     * @var      object
54     * @access    private
55     */
56     private $classname;
57
58    /**
59     * Stores the Propel peer instance
60     *
61     * @var      object
62     * @access    private
63     */
64     private $peer;
65
66    /**
67     * Inits the storage from a Propel peer
68     *
69     * Sets the name of the Propel object classname, creates the Propel peer and
70     * sets the primary field to 'Id'.
71     *
72     * @access    public
73     * @param    string  $peername  The classname of the Propel peer
74     */
75     public function setStorageLocation($peername)
76     {
77         $this->peer = new $peername();
78         $this->classname = array_pop($tmp = explode('.', $this->peer->getOMClass()));
79
80         $primary = array();
81         $object = new $this->classname();
82         foreach($object->buildPkeyCriteria()->keys() as $key) {
83             $primary[] = BasePeer::translateFieldName(
84                 $this->classname, $key, 'colName', 'phpName');
85         }
86         $this->setPrimaryField($primary);
87     }
88
89    /**
90     * Returns a Creole Criteria object for the object lookup
91     *
92     * @access    public
93     * @param    array  $values  The values to create the Criteria
94     * @return   The Criteria object
95     */
96     private function getCriteria($values)
97     {
98         $object = new $this->classname();
99         $primary = $this->getPrimary($values);
100         $object->fromArray($primary);
101         return $object->buildPkeyCriteria();
102     }
103
104    /**
105     * get an entry
106     *
107     * This tries to find an entry in the storage container
108     * that matches the current data that has been set in the
109     * form and populates the form with the data of this
110     * entry
111     *
112     * @access    public
113     * @param    object patForms        patForms object that should be stored
114     * @return    boolean                true on success
115     */
116     public function loadEntry(&$form)
117     {
118         if(!$object = $this->_entryExists($form->getValues())) {
119             // entry does not exists (why return an array here??)
120             return array();
121         }
122
123         $form->setValues($object->toArray());
124         return true;
125     }
126
127    /**
128     * Lets the Propel peer validate the form data.
129     *
130     * This method gets called by the patForms instance by being registered
131     * to its onValidate event. It should not be necessary to call it this
132     * method otherwise.
133     *
134     * @access    public
135     * @param    object  $form  The form
136     * @return   True or false, the validation result
137     */
138     public function validateEntry(&$form)
139     {
140         if (!$object = $this->_entryExists($form->getValues())) {
141             $object = new $this->classname();
142         }
143         $object->fromArray($form->getValues());
144         $result = $object->validate();
145
146         if ($result !== true) {
147             $peer = $object->getPeer();
148             foreach($result as $colname => $error) {
149                 $name = $peer->translateFieldname($colname, 'colName', 'phpName');
150                 $element = $form->getElement($name);
151                 $element->addValidatorErrorCodes(array(
152                     'C' => array(
153                         1 => $error->getMessage(),
154                     ),
155                 ), 1000);
156                 $element->addValidationError(1001);
157             }
158             return false;
159         }
160         return true;
161     }
162
163    /**
164     * adds an entry to the storage
165     *
166     * @param    object patForms        patForms object that should be stored
167     * @return    boolean                true on success
168     */
169     public function _addEntry(&$form)
170     {
171         $object = new $this->classname();
172         $object->fromArray($form->getValues());
173         $object->save();
174
175         $form->setValues($object->toArray(), true);
176         return true;
177     }
178
179    /**
180     * updates an entry in the storage
181     *
182     * @param    object patForms        patForms object that should be stored
183     * @return    boolean                true on success
184     */
185     public function _updateEntry(&$form, $primary)
186     {
187         $object = $this->_entryExists($form->getValues());
188         $object->fromArray($form->getValues());
189         $object->save();
190
191         $form->setValues($object->toArray(), true);
192         return true;
193     }
194
195    /**
196     * check, whether an entry exists
197     *
198     * This method gets called multiple times, e.g. when an existing
199     * object gets updated. We'll therefor cache results locally using
200     * a criteria string representation as hash.
201     *
202     * @access    private
203     * @param    array
204     */
205     public function _entryExists($values)
206     {
207         static $objects;
208         $criteria = $this->getCriteria($values);
209         $hash = $criteria->toString();
210
211         if (isset($objects[$hash])) {
212             return $objects[$hash];
213         }
214
215         $objects[$hash] = $this->peer->doSelectOne($criteria);
216
217         if(empty($objects[$hash])) {
218             return false;
219         }
220         return $objects[$hash];
221     }
222 }
223 ?>
Note: See TracBrowser for help on using the browser.