Subversion Repositories PHPX

Rev

Rev 51 | Rev 53 | 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
   *
38
   * @type array
39
   */
40
  protected $_persistentProperties = array('id');
41
 
42
        /**
27 PointedEar 43
   * Creates a new model object
44
   *
51 PointedEar 45
   * @see AbstractModel::__construct()
27 PointedEar 46
   */
51 PointedEar 47
  public function __construct (array $data = null, array $mapping = null)
27 PointedEar 48
  {
49
    parent::__construct($data, $mapping);
50
  }
51 PointedEar 51
 
52
  public function getPersistentTable ()
53
  {
54
        if (is_string($this->_persistentTable))
55
        {
56
                /* Call setter to convert to Table */
57
                $this->persistentTable = $this->_persistentTable;
58
        }
59
 
60
        return $this->_persistentTable;
61
  }
62
 
63
  public function setPersistentTable ($value)
64
  {
65
        if ($value instanceof Table)
66
        {
67
                $this->_persistentTable = $value;
68
        }
69
        else
70
        {
71
                $table = new $value();
52 PointedEar 72
                if (!($table instanceof Db\Table))
51 PointedEar 73
                {
74
                        throw new \InvalidArgumentException(
52 PointedEar 75
                                'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
76
                                . $value
51 PointedEar 77
                        );
78
                }
79
 
80
                $this->_persistentTable = $table;
81
        }
82
  }
83
 
27 PointedEar 84
  /**
51 PointedEar 85
   * Returns an array for database queries containing the
86
   * property values of this object, using the specified
87
   * property-to-column mapping.
88
   *
89
   * @param array $propertyNames = null
90
   *   Names of the properties that should be included.
91
   *   The default is to include all persistent properties.
92
   * @return array
27 PointedEar 93
   */
51 PointedEar 94
  public function getPropertyArray (array $propertyNames = null)
27 PointedEar 95
  {
51 PointedEar 96
        $a = array();
97
 
98
        if ($propertyNames === null)
99
        {
100
                $propertyNames = $this->_persistentProperties;
101
        }
102
 
103
        foreach ($propertyNames as $propertyName => $columnName)
104
        {
105
                if (is_numeric($propertyName))
106
                {
107
                        $propertyName = $columnName;
108
                }
109
 
110
                $a[$columnName] = $this->$propertyName;
111
        }
112
 
113
        return $a;
27 PointedEar 114
  }
51 PointedEar 115
 
116
  /**
117
   * Finds the record for the model object in a database, fills
118
   * the object with missing data, and returns the result.
119
   *
120
   * @see Table::find(Model)
121
   * @return Model|null
122
   *   This object filled with missing data, or <code>null</code>
123
   *   if there is no data for this object
124
   */
125
  public function find ()
126
  {
127
    $result = $this->persistentTable->find(
52 PointedEar 128
        $this->{$this->_persistentId});
51 PointedEar 129
    if ($result)
130
    {
131
        return $this->map($result);
132
    }
133
 
134
    return null;
135
  }
136
 
137
  /**
138
   * Saves a model object in the <code>Table</code>
139
   *
140
   * @param array $propertyNames = null
141
   *   Names of the properties whose values should be saved
142
   *   in the database.  The default is to save the values
143
   *   of all persistent properties.
144
   * @return boolean
145
   * @see Model::getPropertyArray()
146
   * @see Table::updateOrInsert()
147
   */
148
  public function save (array $propertyNames = null)
149
  {
150
        return $this->persistentTable->updateOrInsert(
151
                $this->getPropertyArray($propertyNames), array(
152
                                $this->persistentTable->id => $this->{$this->_persistentId}
153
                )
154
        );
155
  }
156
 
157
  /**
158
   * Deletes a model object from the <code>Table</code>
159
   *
160
   * @return bool
161
   * @see Table::delete()
162
   */
163
  public function delete ()
164
  {
165
        return $this->persistentTable->delete($this->{$this->_persistentId});
166
  }
27 PointedEar 167
}