From: Chuck Hagenbuch Date: Tue, 16 Dec 2008 03:01:00 +0000 (-0500) Subject: start working on horde_db version of rdo X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=845751977c91f0ef5ef086a73b2ccbcb0cb9e88d;p=horde.git start working on horde_db version of rdo --- diff --git a/framework/Rdo/lib/Horde/Rdo/Mapper.php b/framework/Rdo/lib/Horde/Rdo/Mapper.php index e7cb7aafd..4291513ca 100644 --- a/framework/Rdo/lib/Horde/Rdo/Mapper.php +++ b/framework/Rdo/lib/Horde/Rdo/Mapper.php @@ -16,8 +16,8 @@ * $inflector - The Horde_Support_Inflector this mapper uses to singularize * and pluralize PHP class, database table, and database field/key names. * - * $model - The Horde_Rdo_Model object describing the main table of - * this entity. + * $table - The Horde_Db_Adapter_Abstract_TableDefinition object describing + * the main table of this entity. * * @category Horde * @package Horde_Rdo @@ -96,17 +96,17 @@ abstract class Horde_Rdo_Mapper implements Countable { * inflector: The Horde_Support_Inflector this mapper uses to singularize * and pluralize PHP class, database table, and database field/key names. * - * model: The Horde_Rdo_Model object describing the table or view - * this Mapper manages. + * table: The Horde_Db_Adapter_Abstract_TableDefinition object describing + * the table or view this Mapper manages. * * fields: Array of all field names that are loaded up front - * (eager loading) from the Model. + * (eager loading) from the table. * * lazyFields: Array of fields that are only loaded when accessed. * - * relationships: Array of relationships to other Models. + * relationships: Array of relationships to other Mappers. * - * lazyRelationships: Array of relationships to other Models which + * lazyRelationships: Array of relationships to other Mappers which * are only loaded when accessed. * * @param string $key Property name to fetch @@ -123,13 +123,8 @@ abstract class Horde_Rdo_Mapper implements Countable { case 'inflector': return Horde_Rdo::getInflector(); - case 'model': - $this->model = new Horde_Rdo_Model; - if ($this->_table) { - $this->model->table = $this->_table; - } else { - $this->model->table = $this->mapperToTable(); - } + case 'table': + $table = $this->_table ? $this->_table : $this->mapperToTable(); $this->model->load($this); return $this->model; diff --git a/framework/Rdo/lib/Horde/Rdo/Model.php b/framework/Rdo/lib/Horde/Rdo/Model.php deleted file mode 100644 index 83d6a896b..000000000 --- a/framework/Rdo/lib/Horde/Rdo/Model.php +++ /dev/null @@ -1,119 +0,0 @@ -adapter->loadModel($this); - } - - /** - */ - public static function __set_state($properties) - { - $model = new Horde_Rdo_Model(); - foreach ($properties as $key => $val) { - $model->$key = $val; - } - } - - /** - */ - public function hasField($field) - { - return isset($this->_fields[$field]); - } - - /** - */ - public function addField($field, $params = array()) - { - $params = array_merge(array('key' => null, 'null' => false), $params); - - if (!strncasecmp($params['null'], 'n', 1)) { - $params['null'] = false; - } elseif (!strncasecmp($params['null'], 'y', 1)) { - $params['null'] = true; - } - - $this->_fields[$field] = $params; - if (isset($params['type'])) { - $this->setFieldType($field, $params['type']); - } - } - - /** - */ - public function getField($field) - { - return isset($this->_fields[$field]) ? $this->_fields[$field] : null; - } - - /** - */ - public function getFields() - { - return $this->_fields; - } - - /** - */ - public function listFields() - { - return array_keys($this->_fields); - } - - /** - */ - public function setFieldType($field, $rawtype) - { - if (stripos($rawtype, 'int') !== false) { - $this->_fields[$field]['type'] = self::INTEGER; - } elseif (stripos($rawtype, 'char') !== false) { - $this->_fields[$field]['type'] = self::STRING; - } elseif (stripos($rawtype, 'float') !== false - || stripos($rawtype, 'decimal') !== false) { - $this->_fields[$field]['type'] = self::NUMBER; - } elseif ($rawtype == 'text') { - $this->_fields[$field]['type'] = self::TEXT; - } - } - -} diff --git a/framework/Rdo/lib/Horde/Rdo/Query/Builder.php b/framework/Rdo/lib/Horde/Rdo/Query/Builder.php index 36eea1258..1623e06fd 100644 --- a/framework/Rdo/lib/Horde/Rdo/Query/Builder.php +++ b/framework/Rdo/lib/Horde/Rdo/Query/Builder.php @@ -66,7 +66,7 @@ abstract class Horde_Rdo_Query_Builder { if (count($parts) == 1) { $fields[] = $field; } else { - $fields[] = str_replace('.@', '.', $field) . ' AS ' . $this->quoteColumnName($parts[0] . '@' . $parts[1]); + $fields[] = str_replace('.@', '.', $field) . ' AS ' . $query->mapper->adapter->quoteColumnName($parts[0] . '@' . $parts[1]); } } @@ -112,7 +112,7 @@ abstract class Horde_Rdo_Query_Builder { } $clause = $query->relationships[$rel]['table'] . '.' . $field . ' ' . $this->getTest($test['test']); } else { - $clause = $query->mapper->model->table . '.' . $this->quoteColumnName($test['field']) . ' ' . $this->getTest($test['test']); + $clause = $query->mapper->model->table . '.' . $query->mapper->adapter->quoteColumnName($test['field']) . ' ' . $this->getTest($test['test']); } if ($test['value'] instanceof Horde_Rdo_Query_Literal) { diff --git a/framework/Rdo/package.xml b/framework/Rdo/package.xml new file mode 100644 index 000000000..5ca5fa398 --- /dev/null +++ b/framework/Rdo/package.xml @@ -0,0 +1,72 @@ + + + Rdo + pear.horde.org + Rampage Data Objects + Lightweight ORM layer + + + Chuck Hagenbuch + chuck + chuck@horde.org + yes + + 2008-03-15 + + + 0.3.0 + 0.3.0 + + + beta + beta + + LGPL + Major improvements including inflection, eager or lazy loading of relationships and fields, and more. + + + + + + + + + + + + + + + + + + + + + + + + + 5.2.0 + + + 1.5.0 + + + + + + + + + + + + + + + + diff --git a/framework/Rdo/rdo.php b/framework/Rdo/rdo.php new file mode 100644 index 000000000..56755fc0e --- /dev/null +++ b/framework/Rdo/rdo.php @@ -0,0 +1,87 @@ +count(); +echo "# users: $userCount\n"; + +// Get the number of new users in May 2005 +//$userCount = $um->count('created > \'2005-05-01\' AND created <= \'2005-05-31\''); +//echo "# new: $userCount\n"; + +// Check if id 1 exists. +$exists = $um->exists(1); +echo "exists: " . ($exists ? 'yes' : 'no') . "\n"; + +// Look for Alice +$userTwo = $um->find(Horde_Rdo::FIND_FIRST, array('name' => 'Alice')); +if ($userTwo) { + echo "Found Alice: id $userTwo->id\n"; +} else { + echo "No Alice found, creating:\n"; + $userOne = $um->create(array('name' => 'Alice', 'phone' => '212-555-6565')); + $userOneId = $userOne->id; + echo "Created new user with id: $userOneId\n"; +} + +// Change the name of the user and save. +if ($userTwo) { + $userTwo->name = 'Bob'; + $result = $userTwo->save(); + var_dump($result); +} + +// List all users. +echo "Looking for all:\n"; +foreach ($um->find(Horde_Rdo::FIND_ALL) as $userOb) { + echo " (" . $userOb->id . ") " . $userOb->name . "\n"; +} + +// Fetch id 2. +//$user = $um->find(2); +// Try to delete it. +//$result = $user->delete(); +//var_dump($result); + +/* +// $user->billingAddresses is an Iterator. +foreach ($user->billingAddresses as $billingAddress) { + echo $billingAddress->zipCode . "\n"; +} + +if ($user->favorite) { + echo $user->favorite->name . "\n"; +} else { + $user->favorite = new User(array('name' => 'Charles')); +} +*/