3,44 → 3,165 |
require_once __DIR__ . '/AbstractModel.php'; |
|
/** |
* Abstract model class |
* Abstract model class for Object-Relational Mapping |
* |
* Provides basic setters and getters for protected/private properties |
* and a constructor to initialize properties using setters and getters. |
* Provides simple mapping of a model object to records of |
* a table of a relational database. |
* |
* @author Thomas Lahn |
*/ |
abstract class Model extends AbstractModel |
{ |
/* ORM */ |
const persistentPrimaryKey = 'id'; |
/** |
* The <code>Table</code> for instances of this model |
* |
* @type Table|string |
*/ |
protected $_persistentTable; |
|
/** |
* @var Adapter |
* The name(s) of the property or properties whose value(s) |
* identify this object in the <code>Table</code>. They are |
* used for comparing against the primary key column(s) of |
* the <code>Table</code>. |
* |
* @type string|array[string] |
*/ |
protected static $persistentAdapter; |
protected $_persistentId = 'id'; |
|
/** |
* The names of the properties that should be used in database |
* queries, and their mapping to the columns of |
* the <code>Table</code>, if specified (keys are property names, |
* values are column names, or both if the key is numeric). |
* |
* @type array |
*/ |
protected $_persistentProperties = array('id'); |
|
/** |
* Creates a new model object |
* |
* @param array $data Initialization data (optional) |
* @param array $mapping Mapping for initialization data (optional) |
* @see AbstractModel::__construct() |
*/ |
public function __construct(array $data = null, array $mapping = null) |
{ |
$this->setAdapter(); |
|
parent::__construct($data, $mapping); |
} |
|
public function getPersistentTable () |
{ |
if (is_string($this->_persistentTable)) |
{ |
/* Call setter to convert to Table */ |
$this->persistentTable = $this->_persistentTable; |
} |
|
return $this->_persistentTable; |
} |
|
public function setPersistentTable ($value) |
{ |
if ($value instanceof Table) |
{ |
$this->_persistentTable = $value; |
} |
else |
{ |
$table = new $value(); |
if (!($table instanceof Table)) |
{ |
throw new \InvalidArgumentException( |
'Expected Table instance or string for table name, saw ' |
. (\get_class($value) || \gettype($value)) |
); |
} |
|
$this->_persistentTable = $table; |
} |
} |
|
/** |
* Finds the record for the model object in a database, and fills the object |
* with missing data |
* @see Adapter::find(Model) |
* Returns an array for database queries containing the |
* property values of this object, using the specified |
* property-to-column mapping. |
* |
* @param array $propertyNames = null |
* Names of the properties that should be included. |
* The default is to include all persistent properties. |
* @return array |
*/ |
public function getPropertyArray (array $propertyNames = null) |
{ |
$a = array(); |
|
if ($propertyNames === null) |
{ |
$propertyNames = $this->_persistentProperties; |
} |
|
foreach ($propertyNames as $propertyName => $columnName) |
{ |
if (is_numeric($propertyName)) |
{ |
$propertyName = $columnName; |
} |
|
$a[$columnName] = $this->$propertyName; |
} |
|
return $a; |
} |
|
/** |
* Finds the record for the model object in a database, fills |
* the object with missing data, and returns the result. |
* |
* @see Table::find(Model) |
* @return Model|null |
* This object filled with missing data, or <code>null</code> |
* if there is no data for this object |
*/ |
public function find() |
{ |
$class = get_class($this); |
return $class::$persistentAdapter->find($this); |
$result = $this->persistentTable->find( |
$this->{$this->__persistentId}); |
if ($result) |
{ |
return $this->map($result); |
} |
|
return null; |
} |
|
/** |
* Saves a model object in the <code>Table</code> |
* |
* @param array $propertyNames = null |
* Names of the properties whose values should be saved |
* in the database. The default is to save the values |
* of all persistent properties. |
* @return boolean |
* @see Model::getPropertyArray() |
* @see Table::updateOrInsert() |
*/ |
public function save (array $propertyNames = null) |
{ |
return $this->persistentTable->updateOrInsert( |
$this->getPropertyArray($propertyNames), array( |
$this->persistentTable->id => $this->{$this->_persistentId} |
) |
); |
} |
|
/** |
* Deletes a model object from the <code>Table</code> |
* |
* @return bool |
* @see Table::delete() |
*/ |
public function delete () |
{ |
return $this->persistentTable->delete($this->{$this->_persistentId}); |
} |
} |