Rev 52 | Rev 54 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 27 | PointedEar | 1 | <?php | 
        
| 2 | |||
| 52 | PointedEar | 3 | namespace PointedEars\PHPX;  | 
        
| 29 | PointedEar | 4 | |
| 27 | PointedEar | 5 | /** | 
        
| 51 | PointedEar | 6 | * Abstract model class for Object-Relational Mapping | 
        
| 27 | PointedEar | 7 | * | 
        
| 51 | PointedEar | 8 | * Provides simple mapping of a model object to records of | 
        
| 9 | * a table of a relational database. | 
        ||
| 27 | PointedEar | 10 | * | 
        
| 11 | * @author Thomas Lahn | 
        ||
| 12 | */ | 
        ||
| 52 | PointedEar | 13 | abstract class Model extends \PointedEars\PHPX\AbstractModel  | 
        
| 27 | PointedEar | 14 | { | 
        
| 15 |   /** | 
        ||
| 51 | PointedEar | 16 |    * The <code>Table</code> for instances of this model | 
        
| 17 |    * | 
        ||
| 18 |    * @type Table|string | 
        ||
| 27 | PointedEar | 19 |    */ | 
        
| 51 | PointedEar | 20 | protected $_persistentTable;  | 
        
| 21 | |||
| 27 | PointedEar | 22 |   /** | 
        
| 51 | PointedEar | 23 |    * The name(s) of the property or properties whose value(s) | 
        
| 24 |    * identify this object in the <code>Table</code>.  They are | 
        ||
| 25 |    * used for comparing against the primary key column(s) of | 
        ||
| 26 |    * the <code>Table</code>. | 
        ||
| 27 |    * | 
        ||
| 28 |    * @type string|array[string] | 
        ||
| 29 |    */ | 
        ||
| 30 | protected $_persistentId = 'id';  | 
        ||
| 31 | |||
| 32 |   /** | 
        ||
| 33 |    * The names of the properties that should be used in database | 
        ||
| 34 |    * queries, and their mapping to the columns of | 
        ||
| 35 |    * the <code>Table</code>, if specified (keys are property names, | 
        ||
| 36 |    * values are column names, or both if the key is numeric). | 
        ||
| 37 |    * | 
        ||
| 53 | PointedEar | 38 |    * NOTE: It should not be necessary to include the | 
        
| 39 |    * <code>persistentId</code> property value here.  If an object | 
        ||
| 40 |    * is not in the database, it should be assigned an ID | 
        ||
| 41 |    * automatically when saved; if it is in the database, | 
        ||
| 42 |    * you already have its ID as you searched by it. | 
        ||
| 43 |    * | 
        ||
| 51 | PointedEar | 44 |    * @type array | 
        
| 45 |    */ | 
        ||
| 53 | PointedEar | 46 | protected $_persistentProperties = array();  | 
        
| 51 | PointedEar | 47 | |
| 48 |         /** | 
        ||
| 27 | PointedEar | 49 |    * Creates a new model object | 
        
| 50 |    * | 
        ||
| 51 | PointedEar | 51 |    * @see AbstractModel::__construct() | 
        
| 27 | PointedEar | 52 |    */ | 
        
| 51 | PointedEar | 53 | public function __construct (array $data = null, array $mapping = null)  | 
        
| 27 | PointedEar | 54 |   { | 
        
| 55 | parent::__construct($data, $mapping);  | 
        ||
| 56 |   } | 
        ||
| 51 | PointedEar | 57 | |
| 58 | public function getPersistentTable ()  | 
        ||
| 59 |   { | 
        ||
| 60 | if (is_string($this->_persistentTable))  | 
        ||
| 61 |         { | 
        ||
| 62 |                 /* Call setter to convert to Table */ | 
        ||
| 63 | $this->persistentTable = $this->_persistentTable;  | 
        ||
| 64 |         } | 
        ||
| 65 | |||
| 66 | return $this->_persistentTable;  | 
        ||
| 67 |   } | 
        ||
| 68 | |||
| 69 | public function setPersistentTable ($value)  | 
        ||
| 70 |   { | 
        ||
| 71 | if ($value instanceof Table)  | 
        ||
| 72 |         { | 
        ||
| 73 | $this->_persistentTable = $value;  | 
        ||
| 74 |         } | 
        ||
| 75 |         else | 
        ||
| 76 |         { | 
        ||
| 77 | $table = new $value();  | 
        ||
| 52 | PointedEar | 78 | if (!($table instanceof Db\Table))  | 
        
| 51 | PointedEar | 79 |                 { | 
        
| 80 | throw new \InvalidArgumentException(  | 
        ||
| 52 | PointedEar | 81 |                                 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: ' | 
        
| 82 | . $value  | 
        ||
| 51 | PointedEar | 83 | );  | 
        
| 84 |                 } | 
        ||
| 85 | |||
| 86 | $this->_persistentTable = $table;  | 
        ||
| 87 |         } | 
        ||
| 88 |   } | 
        ||
| 89 | |||
| 27 | PointedEar | 90 |   /** | 
        
| 51 | PointedEar | 91 |    * Returns an array for database queries containing the | 
        
| 92 |    * property values of this object, using the specified | 
        ||
| 93 |    * property-to-column mapping. | 
        ||
| 94 |    * | 
        ||
| 95 |    * @param array $propertyNames = null | 
        ||
| 96 |    *   Names of the properties that should be included. | 
        ||
| 97 |    *   The default is to include all persistent properties. | 
        ||
| 98 |    * @return array | 
        ||
| 27 | PointedEar | 99 |    */ | 
        
| 51 | PointedEar | 100 | public function getPropertyArray (array $propertyNames = null)  | 
        
| 27 | PointedEar | 101 |   { | 
        
| 51 | PointedEar | 102 | $a = array();  | 
        
| 103 | |||
| 104 | if ($propertyNames === null)  | 
        ||
| 105 |         { | 
        ||
| 106 | $propertyNames = $this->_persistentProperties;  | 
        ||
| 107 |         } | 
        ||
| 108 | |||
| 109 | foreach ($propertyNames as $propertyName => $columnName)  | 
        ||
| 110 |         { | 
        ||
| 111 | if (is_numeric($propertyName))  | 
        ||
| 112 |                 { | 
        ||
| 113 | $propertyName = $columnName;  | 
        ||
| 114 |                 } | 
        ||
| 115 | |||
| 116 | $a[$columnName] = $this->$propertyName;  | 
        ||
| 117 |         } | 
        ||
| 118 | |||
| 119 | return $a;  | 
        ||
| 27 | PointedEar | 120 |   } | 
        
| 51 | PointedEar | 121 | |
| 122 |   /** | 
        ||
| 53 | PointedEar | 123 |    * Finds the record for the model object in the table, fills | 
        
| 51 | PointedEar | 124 |    * the object with missing data, and returns the result. | 
        
| 125 |    * | 
        ||
| 126 |    * @see Table::find(Model) | 
        ||
| 127 |    * @return Model|null | 
        ||
| 128 |    *   This object filled with missing data, or <code>null</code> | 
        ||
| 129 |    *   if there is no data for this object | 
        ||
| 130 |    */ | 
        ||
| 131 | public function find ()  | 
        ||
| 132 |   { | 
        ||
| 53 | PointedEar | 133 | $result = $this->persistentTable->find($this->{$this->_persistentId});  | 
        
| 51 | PointedEar | 134 | if ($result)  | 
        
| 135 |     { | 
        ||
| 136 | return $this->map($result);  | 
        ||
| 137 |     } | 
        ||
| 138 | |||
| 139 | return null;  | 
        ||
| 140 |   } | 
        ||
| 141 | |||
| 142 |   /** | 
        ||
| 53 | PointedEar | 143 |    * Saves the model object in the table | 
        
| 51 | PointedEar | 144 |    * | 
        
| 145 |    * @param array $propertyNames = null | 
        ||
| 146 |    *   Names of the properties whose values should be saved | 
        ||
| 147 |    *   in the database.  The default is to save the values | 
        ||
| 148 |    *   of all persistent properties. | 
        ||
| 149 |    * @return boolean | 
        ||
| 150 |    * @see Model::getPropertyArray() | 
        ||
| 151 |    * @see Table::updateOrInsert() | 
        ||
| 152 |    */ | 
        ||
| 153 | public function save (array $propertyNames = null)  | 
        ||
| 154 |   { | 
        ||
| 53 | PointedEar | 155 | $table = $this->persistentTable;  | 
        
| 156 | $idPropertyName = $this->_persistentId;  | 
        ||
| 157 | |||
| 158 | $result = $table->updateOrInsert(  | 
        ||
| 159 | $this->getPropertyArray($propertyNames),  | 
        ||
| 160 | array(  | 
        ||
| 161 | $table->id => $this->$idPropertyName  | 
        ||
| 51 | PointedEar | 162 |                 ) | 
        
| 163 | );  | 
        ||
| 53 | PointedEar | 164 | |
| 165 | if ($result && ($lastInsertId = $table->lastInsertId))  | 
        ||
| 166 |         { | 
        ||
| 167 | $this->$idPropertyName = $lastInsertId;  | 
        ||
| 168 |         } | 
        ||
| 169 | |||
| 170 | return $result;  | 
        ||
| 51 | PointedEar | 171 |   } | 
        
| 172 | |||
| 173 |   /** | 
        ||
| 53 | PointedEar | 174 |    * Inserts the model object into the table | 
        
| 175 |    * | 
        ||
| 176 |    * @param array $propertyNames = null | 
        ||
| 177 |    *   Names of the properties whose values should be insert | 
        ||
| 178 |    *   in the database.  The default is to insert the values | 
        ||
| 179 |    *   of all persistent properties. | 
        ||
| 180 |    * @return boolean | 
        ||
| 181 |    * @see Model::getPropertyArray() | 
        ||
| 182 |    * @see Table::insert() | 
        ||
| 183 |    */ | 
        ||
| 184 | public function insert (array $propertyNames = null)  | 
        ||
| 185 |   { | 
        ||
| 186 | $table = $this->persistentTable;  | 
        ||
| 187 | $idPropertyName = $this->_persistentId;  | 
        ||
| 188 | |||
| 189 | $result = $table->insert($this->getPropertyArray($propertyNames));  | 
        ||
| 190 | |||
| 191 | if ($result && ($lastInsertId = $table->lastInsertId))  | 
        ||
| 192 |         { | 
        ||
| 193 | $this->$idPropertyName = $lastInsertId;  | 
        ||
| 194 |         } | 
        ||
| 195 | |||
| 196 | return $result;  | 
        ||
| 197 |   } | 
        ||
| 198 | |||
| 199 |   /** | 
        ||
| 51 | PointedEar | 200 |    * Deletes a model object from the <code>Table</code> | 
        
| 201 |    * | 
        ||
| 202 |    * @return bool | 
        ||
| 203 |    * @see Table::delete() | 
        ||
| 204 |    */ | 
        ||
| 205 | public function delete ()  | 
        ||
| 206 |   { | 
        ||
| 207 | return $this->persistentTable->delete($this->{$this->_persistentId});  | 
        ||
| 208 |   } | 
        ||
| 27 | PointedEar | 209 | } |