假设我有一个实体(一块石头),我有一些更具体的实体,它们扩展了基本的本质——例如一块会说话的石头,它们有一些不同的属性和方法。一块石头的所有特性都存储在同一个表中的数据库中。特定对象的所有特定属性都序列化存储在表的字段"specific_options"中。我有"石头"类和"Talking_Stone"类,它扩展了基本的石头类。我需要根据从数据库中获得的数据创建一个石头对象。
采用这种方法的最佳做法是什么?
到目前为止我得到的:
class Stone_Data {
...
public static function get_item( $id ) {
$sql = 'SELECT * FROM `' . self::$table . '` WHERE `id`=' . ( int ) $id;
$item = self::$db->get_row( $sql );
return $item;
}
...
}
class Stone_Factory {
...
public static function create( $id = null ) {
// if the stone's type is set
if ( !is_null( $id ) && !is_null( $data = Stone_Data::get_item( $id ) ) && !empty( $data['type'] ) ) {
// create a stone of this type
$class_name = ucfirst( $data['type'] ) . '_Stone';
return new $class_name( $data );
}
// otherwise create a basic stone
return new Stone;
}
...
}
class Stone {
private $data = array(
...
'specific_options' => '',
...
);
...
public function __construct( $data = array() ) {
$this->set( $data );
}
...
public function __set( $name, $value ) {
if ( array_key_exists( $name, $this->data ) ) {
// serialize if the value is an array or an object
if ( is_array( $value ) || is_object( $value ) ) {
$value = serialize( $value );
}
$this->data[$name] = $value;
}
}
public function set( $data = array() ) {
foreach ( $data as $key => $value ) {
// serialize if the value is an array or an object
if ( is_array( $value ) || is_object( $value ) ) {
$data[$key] = serialize( $value );
}
}
$this->data = array_merge( $this->data, $data );
return $this;
}
...
}
class Talking_Stone extends Stone {
...
public function __construct( $data = array() ) {
parent::__construct( $data );
// $this->type = 'talking'
$this->specific_options->language = 'French';
...
}
...
}
但不知怎么的,感觉不太对劲。。
您想要使用一个存储库类:
class StoneRepository
{
private $stoneFactory;
public function __construct(StoneFactory $factory) {
$this->stoneFactory = $factory;
}
/**
* @param integer $id
* @return Stone
*/
public function findStoneById($id) {
// Fetch your data from the database
$row = ...;
// Use the factory to create a new stone from the data
// that has already been fetched
$stone = $this->stoneFactory->create($row);
// Return the new stone
return $stone;
}
}
这里最大的优点是将数据库逻辑从模型中分离出来。这非常棒,因为您可以在不了解数据库的情况下获得一个干净的模型。您可以随意进行查询,也可以保持它们的干净和分离。
然后你可以使用它来获取具有给定id的石头,例如,你的控制器:
$factory = new StoneFactory();
$repository = new StoneRepository($factory);
$stone = $repository->findStoneById($id);
正在提取集合
提取藏品同样简单:
class StoneRepository
{
...
public function findAllStones() {
// Again, fetch the data
$rows = ...;
// Transform each row into a Stone object
$collection = array_map(function ($row) {
return $this->stoneFactory->create($row);
}, $rows);
return $collection;
}
}
返回具有关系的对象
您可能迟早要获取一些相关的对象。你可以在这里采取一些方法。最简单的方法是单独获取每种类型的对象,并在控制器中构建模型。例如,如果您想要一个包含相关Stone
对象的Bag
对象:
$bag = $bagRepository->findBagById($bagId);
$stones = $stoneRepository->findStonesByBagId($bagId);
$bag->addStones($stones);
另一种方法是使用join
并在BagRepository
内实际构建Bag
和Stones
。由于它有点复杂,我不打算把它写在这里。
积垢
您还应该使用存储库根据您的模型在数据库中插入/更新/删除数据。同样,它只需添加insert
、update
和delete
方法即可,这些方法接受Stone
对象并将其数据持久化到数据库中。