create first method for CRUD

This commit is contained in:
Manuel Raynaud
2013-01-09 17:23:46 +01:00
parent 4e21c25867
commit c5bed85462
6 changed files with 233 additions and 12 deletions

View File

@@ -22,6 +22,8 @@
/*************************************************************************************/
namespace Thelia\Model\Base;
use Thelia\Exception\MemberAccessException;
/**
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
@@ -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);

View File

@@ -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;
}
}