Subversion Repositories PHPX

Rev

Rev 51 | Rev 53 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 51 Rev 52
1
<?php
1
<?php
2
2
3
require_once __DIR__ . '/AbstractModel.php';
3
namespace PointedEars\PHPX;
4
4
5
/**
5
/**
6
* Abstract model class for Object-Relational Mapping
6
* Abstract model class for Object-Relational Mapping
7
*
7
*
8
* Provides simple mapping of a model object to records of
8
* Provides simple mapping of a model object to records of
9
* a table of a relational database.
9
* a table of a relational database.
10
*
10
*
11
* @author Thomas Lahn
11
* @author Thomas Lahn
12
*/
12
*/
13
abstract class Model extends AbstractModel
13
abstract class Model extends \PointedEars\PHPX\AbstractModel
14
{
14
{
15
  /**
15
  /**
16
   * The <code>Table</code> for instances of this model
16
   * The <code>Table</code> for instances of this model
17
   *
17
   *
18
   * @type Table|string
18
   * @type Table|string
19
   */
19
   */
20
        protected $_persistentTable;
20
        protected $_persistentTable;
21
21
22
  /**
22
  /**
23
   * The name(s) of the property or properties whose value(s)
23
   * The name(s) of the property or properties whose value(s)
24
   * identify this object in the <code>Table</code>.  They are
24
   * identify this object in the <code>Table</code>.  They are
25
   * used for comparing against the primary key column(s) of
25
   * used for comparing against the primary key column(s) of
26
   * the <code>Table</code>.
26
   * the <code>Table</code>.
27
   *
27
   *
28
   * @type string|array[string]
28
   * @type string|array[string]
29
   */
29
   */
30
        protected $_persistentId = 'id';
30
        protected $_persistentId = 'id';
31
31
32
  /**
32
  /**
33
   * The names of the properties that should be used in database
33
   * The names of the properties that should be used in database
34
   * queries, and their mapping to the columns of
34
   * queries, and their mapping to the columns of
35
   * the <code>Table</code>, if specified (keys are property names,
35
   * the <code>Table</code>, if specified (keys are property names,
36
   * values are column names, or both if the key is numeric).
36
   * values are column names, or both if the key is numeric).
37
   *
37
   *
38
   * @type array
38
   * @type array
39
   */
39
   */
40
  protected $_persistentProperties = array('id');
40
  protected $_persistentProperties = array('id');
41
41
42
        /**
42
        /**
43
   * Creates a new model object
43
   * Creates a new model object
44
   *
44
   *
45
   * @see AbstractModel::__construct()
45
   * @see AbstractModel::__construct()
46
   */
46
   */
47
  public function __construct (array $data = null, array $mapping = null)
47
  public function __construct (array $data = null, array $mapping = null)
48
  {
48
  {
49
    parent::__construct($data, $mapping);
49
    parent::__construct($data, $mapping);
50
  }
50
  }
51
51
52
  public function getPersistentTable ()
52
  public function getPersistentTable ()
53
  {
53
  {
54
        if (is_string($this->_persistentTable))
54
        if (is_string($this->_persistentTable))
55
        {
55
        {
56
                /* Call setter to convert to Table */
56
                /* Call setter to convert to Table */
57
                $this->persistentTable = $this->_persistentTable;
57
                $this->persistentTable = $this->_persistentTable;
58
        }
58
        }
59
59
60
        return $this->_persistentTable;
60
        return $this->_persistentTable;
61
  }
61
  }
62
62
63
  public function setPersistentTable ($value)
63
  public function setPersistentTable ($value)
64
  {
64
  {
65
        if ($value instanceof Table)
65
        if ($value instanceof Table)
66
        {
66
        {
67
                $this->_persistentTable = $value;
67
                $this->_persistentTable = $value;
68
        }
68
        }
69
        else
69
        else
70
        {
70
        {
71
                $table = new $value();
71
                $table = new $value();
72
                if (!($table instanceof Table))
72
                if (!($table instanceof Db\Table))
73
                {
73
                {
74
                        throw new \InvalidArgumentException(
74
                        throw new \InvalidArgumentException(
75
                                'Expected Table instance or string for table name, saw '
75
                                'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
76
                                        . (\get_class($value) || \gettype($value))
76
                                . $value
77
                        );
77
                        );
78
                }
78
                }
79
79
80
                $this->_persistentTable = $table;
80
                $this->_persistentTable = $table;
81
        }
81
        }
82
  }
82
  }
83
83
84
  /**
84
  /**
85
   * Returns an array for database queries containing the
85
   * Returns an array for database queries containing the
86
   * property values of this object, using the specified
86
   * property values of this object, using the specified
87
   * property-to-column mapping.
87
   * property-to-column mapping.
88
   *
88
   *
89
   * @param array $propertyNames = null
89
   * @param array $propertyNames = null
90
   *   Names of the properties that should be included.
90
   *   Names of the properties that should be included.
91
   *   The default is to include all persistent properties.
91
   *   The default is to include all persistent properties.
92
   * @return array
92
   * @return array
93
   */
93
   */
94
  public function getPropertyArray (array $propertyNames = null)
94
  public function getPropertyArray (array $propertyNames = null)
95
  {
95
  {
96
        $a = array();
96
        $a = array();
97
97
98
        if ($propertyNames === null)
98
        if ($propertyNames === null)
99
        {
99
        {
100
                $propertyNames = $this->_persistentProperties;
100
                $propertyNames = $this->_persistentProperties;
101
        }
101
        }
102
102
103
        foreach ($propertyNames as $propertyName => $columnName)
103
        foreach ($propertyNames as $propertyName => $columnName)
104
        {
104
        {
105
                if (is_numeric($propertyName))
105
                if (is_numeric($propertyName))
106
                {
106
                {
107
                        $propertyName = $columnName;
107
                        $propertyName = $columnName;
108
                }
108
                }
109
109
110
                $a[$columnName] = $this->$propertyName;
110
                $a[$columnName] = $this->$propertyName;
111
        }
111
        }
112
112
113
        return $a;
113
        return $a;
114
  }
114
  }
115
115
116
  /**
116
  /**
117
   * Finds the record for the model object in a database, fills
117
   * Finds the record for the model object in a database, fills
118
   * the object with missing data, and returns the result.
118
   * the object with missing data, and returns the result.
119
   *
119
   *
120
   * @see Table::find(Model)
120
   * @see Table::find(Model)
121
   * @return Model|null
121
   * @return Model|null
122
   *   This object filled with missing data, or <code>null</code>
122
   *   This object filled with missing data, or <code>null</code>
123
   *   if there is no data for this object
123
   *   if there is no data for this object
124
   */
124
   */
125
  public function find ()
125
  public function find ()
126
  {
126
  {
127
    $result = $this->persistentTable->find(
127
    $result = $this->persistentTable->find(
128
        $this->{$this->__persistentId});
128
        $this->{$this->_persistentId});
129
    if ($result)
129
    if ($result)
130
    {
130
    {
131
        return $this->map($result);
131
        return $this->map($result);
132
    }
132
    }
133
133
134
    return null;
134
    return null;
135
  }
135
  }
136
136
137
  /**
137
  /**
138
   * Saves a model object in the <code>Table</code>
138
   * Saves a model object in the <code>Table</code>
139
   *
139
   *
140
   * @param array $propertyNames = null
140
   * @param array $propertyNames = null
141
   *   Names of the properties whose values should be saved
141
   *   Names of the properties whose values should be saved
142
   *   in the database.  The default is to save the values
142
   *   in the database.  The default is to save the values
143
   *   of all persistent properties.
143
   *   of all persistent properties.
144
   * @return boolean
144
   * @return boolean
145
   * @see Model::getPropertyArray()
145
   * @see Model::getPropertyArray()
146
   * @see Table::updateOrInsert()
146
   * @see Table::updateOrInsert()
147
   */
147
   */
148
  public function save (array $propertyNames = null)
148
  public function save (array $propertyNames = null)
149
  {
149
  {
150
        return $this->persistentTable->updateOrInsert(
150
        return $this->persistentTable->updateOrInsert(
151
                $this->getPropertyArray($propertyNames), array(
151
                $this->getPropertyArray($propertyNames), array(
152
                                $this->persistentTable->id => $this->{$this->_persistentId}
152
                                $this->persistentTable->id => $this->{$this->_persistentId}
153
                )
153
                )
154
        );
154
        );
155
  }
155
  }
156
156
157
  /**
157
  /**
158
   * Deletes a model object from the <code>Table</code>
158
   * Deletes a model object from the <code>Table</code>
159
   *
159
   *
160
   * @return bool
160
   * @return bool
161
   * @see Table::delete()
161
   * @see Table::delete()
162
   */
162
   */
163
  public function delete ()
163
  public function delete ()
164
  {
164
  {
165
        return $this->persistentTable->delete($this->{$this->_persistentId});
165
        return $this->persistentTable->delete($this->{$this->_persistentId});
166
  }
166
  }
167
}
167
}
168
 
168