Subversion Repositories PHPX

Rev

Rev 29 | Rev 52 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29 Rev 51
Line 1... Line 1...
1
<?php
1
<?php
2
2
3
require_once __DIR__ . '/AbstractModel.php';
3
require_once __DIR__ . '/AbstractModel.php';
4
4
5
/**
5
/**
6
* Abstract model class
6
* Abstract model class for Object-Relational Mapping
7
*
7
*
8
* Provides basic setters and getters for protected/private properties
8
* Provides simple mapping of a model object to records of
9
* and a constructor to initialize properties using setters and getters.
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 AbstractModel
14
{
14
{
-
 
15
  /**
-
 
16
   * The <code>Table</code> for instances of this model
-
 
17
   *
-
 
18
   * @type Table|string
15
  /* ORM */
19
   */
-
 
20
        protected $_persistentTable;
-
 
21
-
 
22
  /**
-
 
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
   */
16
  const persistentPrimaryKey = 'id';
30
        protected $_persistentId = 'id';
17
 
31
18
  /**
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
   *
19
   * @var Adapter
38
   * @type array
20
   */
39
   */
21
  protected static $persistentAdapter;
40
  protected $_persistentProperties = array('id');
22
 
41
23
  /**
42
        /**
24
   * Creates a new model object
43
   * Creates a new model object
25
   *
44
   *
26
   * @param array $data     Initialization data (optional)
45
   * @see AbstractModel::__construct()
27
   * @param array $mapping  Mapping for initialization data (optional)
-
 
28
   */
46
   */
29
  public function __construct(array $data = null, array $mapping = null)
47
  public function __construct (array $data = null, array $mapping = null)
30
  {
48
  {
31
    $this->setAdapter();
-
 
32
       
-
 
33
    parent::__construct($data, $mapping);
49
    parent::__construct($data, $mapping);
34
  }
50
  }
35
     
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();
-
 
72
                if (!($table instanceof Table))
-
 
73
                {
-
 
74
                        throw new \InvalidArgumentException(
-
 
75
                                'Expected Table instance or string for table name, saw '
-
 
76
                                        . (\get_class($value) || \gettype($value))
-
 
77
                        );
-
 
78
                }
-
 
79
-
 
80
                $this->_persistentTable = $table;
-
 
81
        }
-
 
82
  }
-
 
83
36
  /**
84
  /**
-
 
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
-
 
93
   */
-
 
94
  public function getPropertyArray (array $propertyNames = null)
-
 
95
  {
-
 
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;
-
 
114
  }
-
 
115
-
 
116
  /**
37
   * Finds the record for the model object in a database, and fills the object
117
   * Finds the record for the model object in a database, fills
38
   * with missing data
118
   * the object with missing data, and returns the result.
-
 
119
   *
39
   * @see Adapter::find(Model)
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
40
   */
124
   */
41
  public function find()
125
  public function find ()
42
  {
126
  {
-
 
127
    $result = $this->persistentTable->find(
-
 
128
        $this->{$this->__persistentId});
-
 
129
    if ($result)
-
 
130
    {
43
    $class = get_class($this);
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
  {
44
    return $class::$persistentAdapter->find($this);
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});
45
  }
166
  }
46
}
167
}