Subversion Repositories PHPX

Rev

Rev 54 | Rev 62 | 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
   */
54 PointedEar 20
        protected static $_persistentTable;
51 PointedEar 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
   */
55 PointedEar 30
        protected static $_persistentId = 'id';
51 PointedEar 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
   */
55 PointedEar 46
  protected static $_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
  {
54 PointedEar 60
        $class = \get_class($this);
61
        if (\is_string($class::$_persistentTable))
51 PointedEar 62
        {
63
                /* Call setter to convert to Table */
54 PointedEar 64
                $this->setPersistentTable($class::$_persistentTable);
51 PointedEar 65
        }
66
 
54 PointedEar 67
        return $class::$_persistentTable;
51 PointedEar 68
  }
69
 
70
  public function setPersistentTable ($value)
71
  {
54 PointedEar 72
        $class = \get_class($this);
51 PointedEar 73
        if ($value instanceof Table)
74
        {
54 PointedEar 75
                $class::$_persistentTable = $value;
51 PointedEar 76
        }
77
        else
78
        {
79
                $table = new $value();
52 PointedEar 80
                if (!($table instanceof Db\Table))
51 PointedEar 81
                {
82
                        throw new \InvalidArgumentException(
52 PointedEar 83
                                'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
84
                                . $value
51 PointedEar 85
                        );
86
                }
87
 
54 PointedEar 88
                $class::$_persistentTable = $table;
51 PointedEar 89
        }
90
  }
91
 
27 PointedEar 92
  /**
51 PointedEar 93
   * Returns an array for database queries containing the
94
   * property values of this object, using the specified
95
   * property-to-column mapping.
96
   *
97
   * @param array $propertyNames = null
98
   *   Names of the properties that should be included.
99
   *   The default is to include all persistent properties.
100
   * @return array
27 PointedEar 101
   */
51 PointedEar 102
  public function getPropertyArray (array $propertyNames = null)
27 PointedEar 103
  {
51 PointedEar 104
        $a = array();
105
 
106
        if ($propertyNames === null)
107
        {
55 PointedEar 108
                $class = \get_class($this);
109
                $propertyNames = $class::$_persistentProperties;
51 PointedEar 110
        }
111
 
112
        foreach ($propertyNames as $propertyName => $columnName)
113
        {
114
                if (is_numeric($propertyName))
115
                {
116
                        $propertyName = $columnName;
117
                }
118
 
119
                $a[$columnName] = $this->$propertyName;
120
        }
121
 
122
        return $a;
27 PointedEar 123
  }
51 PointedEar 124
 
125
  /**
53 PointedEar 126
   * Finds the record for the model object in the table, fills
51 PointedEar 127
   * the object with missing data, and returns the result.
128
   *
129
   * @see Table::find(Model)
130
   * @return Model|null
131
   *   This object filled with missing data, or <code>null</code>
132
   *   if there is no data for this object
133
   */
134
  public function find ()
135
  {
54 PointedEar 136
        $class = \get_class($this);
55 PointedEar 137
    $result = $this->persistentTable->find($this->{$class::$_persistentId});
51 PointedEar 138
    if ($result)
139
    {
140
        return $this->map($result);
141
    }
142
 
143
    return null;
144
  }
145
 
146
  /**
53 PointedEar 147
   * Saves the model object in the table
51 PointedEar 148
   *
149
   * @param array $propertyNames = null
150
   *   Names of the properties whose values should be saved
151
   *   in the database.  The default is to save the values
152
   *   of all persistent properties.
153
   * @return boolean
154
   * @see Model::getPropertyArray()
155
   * @see Table::updateOrInsert()
156
   */
157
  public function save (array $propertyNames = null)
158
  {
53 PointedEar 159
        $table = $this->persistentTable;
55 PointedEar 160
        $class = \get_class($this);
161
        $idPropertyName = $class::$_persistentId;
53 PointedEar 162
 
163
        $result = $table->updateOrInsert(
164
                $this->getPropertyArray($propertyNames),
165
                array(
166
                        $table->id => $this->$idPropertyName
51 PointedEar 167
                )
168
        );
53 PointedEar 169
 
170
        if ($result && ($lastInsertId = $table->lastInsertId))
171
        {
172
                $this->$idPropertyName = $lastInsertId;
173
        }
174
 
175
        return $result;
51 PointedEar 176
  }
177
 
178
  /**
53 PointedEar 179
   * Inserts the model object into the table
180
   *
181
   * @param array $propertyNames = null
182
   *   Names of the properties whose values should be insert
183
   *   in the database.  The default is to insert the values
184
   *   of all persistent properties.
185
   * @return boolean
186
   * @see Model::getPropertyArray()
187
   * @see Table::insert()
188
   */
189
  public function insert (array $propertyNames = null)
190
  {
191
        $table = $this->persistentTable;
55 PointedEar 192
        $class = \get_class($this);
193
        $idPropertyName = $class::$_persistentId;
53 PointedEar 194
 
195
        $result = $table->insert($this->getPropertyArray($propertyNames));
196
 
197
        if ($result && ($lastInsertId = $table->lastInsertId))
198
        {
199
                $this->$idPropertyName = $lastInsertId;
200
        }
201
 
202
        return $result;
203
  }
204
 
205
  /**
51 PointedEar 206
   * Deletes a model object from the <code>Table</code>
207
   *
208
   * @return bool
209
   * @see Table::delete()
210
   */
211
  public function delete ()
212
  {
55 PointedEar 213
        $class = \get_class($this);
214
        return $this->persistentTable->delete($this->{$class::$_persistentId});
51 PointedEar 215
  }
27 PointedEar 216
}