remove _get and _set magic method on Base model

This commit is contained in:
Manuel Raynaud
2013-01-10 16:49:47 +01:00
parent 673d8b13da
commit dc8984b306
4 changed files with 206 additions and 55 deletions

View File

@@ -53,7 +53,8 @@ class ModelBundle extends Bundle
{ {
foreach (DIGenerator::genDiModel(realpath(THELIA_ROOT . "core/lib/Thelia/Model"), array('Base')) as $name => $class) { foreach (DIGenerator::genDiModel(realpath(THELIA_ROOT . "core/lib/Thelia/Model"), array('Base')) as $name => $class) {
$container->register('model.'.$name, $class) $container->register('model.'.$name, $class)
->addArgument(new Reference("database")); ->addArgument(new Reference("database"))
->addArgument(new Reference('service_container'));
} }
} }
} }

View File

@@ -28,7 +28,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* *
* Master class of Thelia's parser. The loop mechnism depends of this parser * Master class of Thelia's parser. The loop mechanism depends of this parser
* *
* From this class all the parser is lunch * From this class all the parser is lunch
* *
@@ -80,11 +80,7 @@ class Parser implements ParserInterface
$config = $this->container->get("model.config"); $config = $this->container->get("model.config");
$results = $config->find(1); var_dump($config->read("tlog_niveau","tutu"));
var_dump($results->delete());
return $this->content; return $this->content;
} }

View File

@@ -22,9 +22,12 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Model\Base; namespace Thelia\Model\Base;
use Thelia\Exception\MemberAccessException; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
*
* Abstract class. All model classes inherit from this class
*
* @author Manuel Raynaud <mraynaud@openstudio.fr> * @author Manuel Raynaud <mraynaud@openstudio.fr>
*/ */
abstract class Base abstract class Base
@@ -40,6 +43,20 @@ abstract class Base
*/ */
protected $db; protected $db;
/**
*
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
*
* Short name of the current class instance
*
* @var string
*/
protected $className;
/** /**
* *
* @var string * @var string
@@ -68,79 +85,137 @@ abstract class Base
"updated_at" "updated_at"
); );
/** /**
*
*
* *
* @param \NotORM $NotORM * @param \NotORM $NotORM
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
*/ */
public function __construct(\NotORM $NotORM) public function __construct(\NotORM $NotORM, ContainerInterface $container)
{ {
$this->db = $NotORM; $this->db = $NotORM;
$this->table = $this->getTableName(); $this->className = $this->getShortClassName();
} $this->table = $this->underscore($this->className);
$this->container = $container;
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']));
} }
/**
*
* return the date and time when the record had been updated.
*
* @return \DateTime
*/
public function getUpdatedAt() public function getUpdatedAt()
{
if ($this->updated_at) {
return new \DateTime($this->updated_at);
} else {
return null;
}
}
/**
*
* return the raw date time when the record had been updated
*
* @return string
*/
public function getRawUpadtedAt()
{ {
return $this->updated_at; return $this->updated_at;
} }
/**
*
* string date time when the record had been updated
*
* @param string $updated_at
*/
public function setUpdatedAt($updated_at) public function setUpdatedAt($updated_at)
{ {
$this->updated_at = $updated_at; $this->updated_at = $updated_at;
} }
/**
*
* return the date and time when the record had been created.
*
* @return \DateTime
*/
public function getCreatedAt() public function getCreatedAt()
{
if ($this->created_at) {
return new \DateTime($this->created_at);
} else {
return null;
}
}
/**
*
* return the raw date time when the record had been created
*
* @return string
*/
public function getRawCreatedAt()
{ {
return $this->created_at; return $this->created_at;
} }
/**
*
* string date time when the record had been updated
*
* @param string $created_at
*/
public function setCreatedAt($created_at) public function setCreatedAt($created_at)
{ {
$this->created_at = $created_at; $this->created_at = $created_at;
} }
/**
*
* return the base properties like created_at, updated_at
*
* @return array
*/
private function getBaseProperties() private function getBaseProperties()
{ {
return $this->baseProperties; return $this->baseProperties;
} }
/**
*
* return the public properties of the current model.
*
* @return array
*/
public function getProperties() public function getProperties()
{ {
return $this->properties; return $this->properties;
} }
/**
*
* return the id of the current record
*
* @return type
*/
public function getId() public function getId()
{ {
return $this->id; return $this->id;
} }
/**
*
* fix the id if needed
*
* @param int $id
*/
public function setId($id) public function setId($id)
{ {
$this->id = $id; $this->id = $id;
} }
/**
*
* @return \NotORM
*/
public function getDb()
{
return $this->db;
}
/** /**
* *
* @return string Name of the current Table * @return string Name of the current Table
@@ -159,26 +234,31 @@ abstract class Base
return $this->db; return $this->db;
} }
private function _get($property) /**
*
* @return Symfony\Component\DependencyInjection\ContainerInterface
*/
public function getContainer()
{ {
if (!property_exists($this, $property)) { return $this->container;
throw new \InvalidArgumentException($property." property does not exists");
}
return $this->$property;
} }
private function _set($property, $value) /**
*
* return the short name (without namespace) of the current instance class name.
*
* @return string
*/
public function getClassName()
{ {
if (!property_exists($this, $property)) { return $this->className;
throw new \InvalidArgumentException($property." property does not exists");
}
$this->$property = $value;
} }
/** /**
* Persist data in current table * Persist data in current table
*
* Same method for saving or updating a record
*
*/ */
public function save() public function save()
{ {
@@ -202,7 +282,7 @@ abstract class Base
* *
* Find record by primary key * Find record by primary key
* *
* @param int $pk * @param int $pk
* @return Object * @return Object
*/ */
public function find($pk) public function find($pk)
@@ -239,7 +319,7 @@ abstract class Base
/** /**
* *
* Find record for a specific column * Find the first record for a specific column
* *
* @param mixed $column column name * @param mixed $column column name
* @param mixed $search value searching * @param mixed $search value searching
@@ -260,6 +340,13 @@ abstract class Base
return $this->parseOneQuery($result); return $this->parseOneQuery($result);
} }
/**
*
* delete the current record
*
* @return int
* @throws \RuntimeException
*/
public function delete() public function delete()
{ {
if ($this->isNew()) { if ($this->isNew()) {
@@ -275,7 +362,7 @@ abstract class Base
/** /**
* *
* @param \NotORM_Result $results * @param \NotORM_Result $results
* @return array * @return array
*/ */
private function parseQuery(\NotORM_Result $results) private function parseQuery(\NotORM_Result $results)
@@ -286,9 +373,10 @@ abstract class Base
// @TODO : change hard code assignation // @TODO : change hard code assignation
array_push($properties, "id"); array_push($properties, "id");
foreach ($results as $result) { foreach ($results as $result) {
$class = new static($this->getConnection()); //$class = new static($this->getConnection());
foreach($properties as $property) $class = $this->getContainer()->get("model.".$this->getClassName());
{
foreach ($properties as $property) {
call_user_func(array($class, "set".ucfirst(self::camelize($property))), $result[$property]); call_user_func(array($class, "set".ucfirst(self::camelize($property))), $result[$property]);
} }
array_push($return, $class); array_push($return, $class);
@@ -323,6 +411,12 @@ abstract class Base
return $values; return $values;
} }
/**
*
* check if the current record is new or not.
*
* @return boolean
*/
public function isNew() public function isNew()
{ {
return $this->getId() ? false:true; return $this->getId() ? false:true;
@@ -332,10 +426,11 @@ abstract class Base
* *
* @return string name of the current table * @return string name of the current table
*/ */
protected function getTableName() protected function getShortClassName()
{ {
$info = new \ReflectionObject($this); $info = new \ReflectionObject($this);
return $this->underscore($info->getShortName());
return $info->getShortName();
} }
/** /**

View File

@@ -1,5 +1,25 @@
<?php <?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Model; namespace Thelia\Model;
use Thelia\Model\Base\Base; use Thelia\Model\Base\Base;
@@ -11,6 +31,45 @@ class Config extends Base
protected $secure; protected $secure;
protected $hidden; protected $hidden;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
}
public function getSecure()
{
return $this->secure;
}
public function setSecure($secure)
{
$this->secure = $secure;
}
public function getHidden()
{
return $this->hidden;
}
public function setHidden($hidden)
{
$this->hidden = $hidden;
}
protected $properties = array( protected $properties = array(
"name", "name",