From c5bed85462af9f395a9f7ca513e1cdd5b2ea6de4 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 9 Jan 2013 17:23:46 +0100 Subject: [PATCH] create first method for CRUD --- core/lib/Thelia/Core/Bundle/NotORMBundle.php | 2 +- core/lib/Thelia/Core/Template/Parser.php | 2 +- core/lib/Thelia/Database/NotORM.php | 5 + .../Exception/MemberAccessException.php | 28 +++ core/lib/Thelia/Model/Base/Base.php | 192 +++++++++++++++++- core/lib/Thelia/Model/Config.php | 16 +- 6 files changed, 233 insertions(+), 12 deletions(-) create mode 100644 core/lib/Thelia/Exception/MemberAccessException.php diff --git a/core/lib/Thelia/Core/Bundle/NotORMBundle.php b/core/lib/Thelia/Core/Bundle/NotORMBundle.php index a138f8d74..c8ed30143 100644 --- a/core/lib/Thelia/Core/Bundle/NotORMBundle.php +++ b/core/lib/Thelia/Core/Bundle/NotORMBundle.php @@ -49,7 +49,7 @@ class NotORMBundle extends Bundle public function build(ContainerBuilder $container) { $config = array( - // \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ); $kernel = $container->get('kernel'); diff --git a/core/lib/Thelia/Core/Template/Parser.php b/core/lib/Thelia/Core/Template/Parser.php index 75b36142a..021740896 100644 --- a/core/lib/Thelia/Core/Template/Parser.php +++ b/core/lib/Thelia/Core/Template/Parser.php @@ -80,7 +80,7 @@ class Parser implements ParserInterface $config = $this->container->get("model.config"); - echo $config->read("toto","tutu"); + var_dump($config->read("tlog_niveau","tutu")); return $this->content; } diff --git a/core/lib/Thelia/Database/NotORM.php b/core/lib/Thelia/Database/NotORM.php index cabc913f4..1e8752fbc 100644 --- a/core/lib/Thelia/Database/NotORM.php +++ b/core/lib/Thelia/Database/NotORM.php @@ -57,6 +57,11 @@ class NotORM extends \NotORM } } + public function getStructure() + { + return $this->structure; + } + /** Get table data * @param string * @param array (["condition"[, array("value")]]) passed to NotORM_Result::where() diff --git a/core/lib/Thelia/Exception/MemberAccessException.php b/core/lib/Thelia/Exception/MemberAccessException.php new file mode 100644 index 000000000..9ceea565e --- /dev/null +++ b/core/lib/Thelia/Exception/MemberAccessException.php @@ -0,0 +1,28 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Exception; + +class MemberAccessException extends \LogicException +{ + +} diff --git a/core/lib/Thelia/Model/Base/Base.php b/core/lib/Thelia/Model/Base/Base.php index ce64164c4..43e6dff59 100644 --- a/core/lib/Thelia/Model/Base/Base.php +++ b/core/lib/Thelia/Model/Base/Base.php @@ -22,6 +22,8 @@ /*************************************************************************************/ namespace Thelia\Model\Base; +use Thelia\Exception\MemberAccessException; + /** * @author Manuel Raynaud */ @@ -63,7 +65,7 @@ abstract class Base */ private $baseProperties = array( "created_at", - "update_at" + "updated_at" ); /** @@ -78,6 +80,18 @@ abstract class Base $this->table = $this->getTableName(); } + public function __call($name, $arguments) { + if (substr($name,0,3) == "get") { + return $this->_get($this->underscore(substr($name,3))); + } + + if (substr($name,0,3) == "set") { + return $this->_set($this->underscore(substr($name,3)), $arguments[0]); + } + $calee = next(debug_backtrace()); + throw new MemberAccessException(sprintf("Call to undefined method %s->%s in %s on line %s", $calee['class'], $name,$calee['file'],$calee['line'])); + } + public function getUpdatedAt() { return $this->updated_at; @@ -127,25 +141,168 @@ abstract class Base return $this->db; } + /** + * + * @return string Name of the current Table + */ public function getTable() { return $this->table; } + /** + * + * @return \NotORM + */ + public function getConnection() + { + return $this->db; + } + + private function _get($property) + { + if (!property_exists($this, $property)) { + throw new \InvalidArgumentException($property." property does not exists"); + } + + return $this->$property; + } + + private function _set($property, $value) + { + if (!property_exists($this, $property)) { + throw new \InvalidArgumentException($property." property does not exists"); + } + + $this->$property = $value; + } + + /** + * Persist data in current table + */ public function save() { - $this->updated_at = $this->created_at = date('Y-m-d H:i:s'); + if ($this->isNew()) { + $this->updated_at = $this->created_at = date('Y-m-d H:i:s'); + } else { + $this->updated_at = date("Y-m-d H:i:s"); + } $values = $this->prepare(); - -// $this->db->insert_update( -// array("id", $this->getId()), -// $values, -// -// ); + $table = $this->getTable(); + $this->getConnection()->$table()->insert_update( + array("id", $this->getId()), + $values, + $values + ); } + /** + * + * Find record by primary key + * + * @param int $pk + * @return \NotORM_Result + */ + public function find($pk) + { + $table = $this->getTable(); + + return $this->getConnection()->$table()->where("id", $pk); + } + + /** + * + * Find record for a specific column + * + * @param mixed $column column name + * @param mixed $search value searching + * @return \NotORM_Result + * @throws \InvalidArgumentException column name cannot be empty + */ + public function findBy($column, $search) + { + + if (empty($column)) { + throw new \InvalidArgumentException("Column name cannot be emtpy"); + } + + $table = $this->getTable(); + + $result = $this->getConnection()->$table()->where($column, $search); + + return $this->parseQuery($result); + } + + /** + * + * Find record for a specific column + * + * @param mixed $column column name + * @param mixed $search value searching + * @return \NotORM_Result + * @throws \InvalidArgumentException column name cannot be empty + */ + public function findOneBy($column, $search) + { + + if (empty($column)) { + throw new \InvalidArgumentException("Column name cannot be emtpy"); + } + + $table = $this->getTable(); + + $result = $this->getConnection()->$table()->where($column, $search)->limit(1); + + $return = $this->parseQuery($result); + + return count($return) ? $return[0] : null ; + } + + public function delete() + { + if ($this->isNew()) { + throw new \RuntimeException("Cannot delete row. id is empty"); + } + + $table = $this->getTable(); + + return $this->getConnection()->$table() + ->where("id", $this->getId()) + ->delete(); + } + + /** + * + * @param \NotORM_Result $results + * @return array + */ + private function parseQuery(\NotORM_Result $results) + { + $return = array(); + $properties = array_merge($this->getBaseProperties(), $this->getProperties()); + + // @TODO : change hard code assignation + array_push($properties, "id"); + foreach ($results as $result) { + $class = new static($this->getConnection()); + foreach($properties as $property) + { + call_user_func(array($class, "set".ucfirst(self::camelize($property))), $result[$property]); + } + array_push($return, $class); + } + + return $return; + } + + /** + * + * prepare an array for persisting data + * + * @return Array + */ private function prepare() { $properties = array_merge($this->getBaseProperties(), $this->getProperties()); @@ -170,7 +327,8 @@ abstract class Base */ protected function getTableName() { - return $this->underscore(__CLASS__); + $info = new \ReflectionObject($this); + return $this->underscore($info->getShortName()); } /** @@ -194,6 +352,22 @@ abstract class Base return strtolower($tmp); } + /** + * Returns a camelized string from a lower case and underscored string by replaceing slash with + * double-colon and upper-casing each letter preceded by an underscore. + * + * @param string $lower_case_and_underscored_word String to camelize. + * + * @return string Camelized string. + */ + public static function camelize($lower_case_and_underscored_word) + { + $tmp = $lower_case_and_underscored_word; + $tmp = self::pregtr($tmp, array('#/(.?)#e' => "'::'.strtoupper('\\1')",'/(^|_|-)+(.)/e' => "strtoupper('\\2')")); + + return $tmp; + } + public static function pregtr($search, $replacePairs) { return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search); diff --git a/core/lib/Thelia/Model/Config.php b/core/lib/Thelia/Model/Config.php index 16ba351bf..ed9d01dcb 100644 --- a/core/lib/Thelia/Model/Config.php +++ b/core/lib/Thelia/Model/Config.php @@ -6,9 +6,23 @@ use Thelia\Model\Base\Base; class Config extends Base { + protected $name; + protected $value; + protected $secure; + protected $hidden; + + + protected $properties = array( + "name", + "value", + "secure", + "hidden" + ); public function read($search, $default) { - return $this->db->config()->where("name",$search)->fetch()?:$default; + $result = $this->findOneBy("name",$search); + + return $result ? $result->name : $default; } }