添加、编辑、查看和删除数据库条目的PHP模式
我想创建一个PHP类来包含某种数据(一篇带有“标题”和“文本”的文章)。此类应用于不同目的:添加、编辑、查看和删除数据库条目的PHP模式,php,database,design-patterns,view,edit,Php,Database,Design Patterns,View,Edit,我想创建一个PHP类来包含某种数据(一篇带有“标题”和“文本”的文章)。此类应用于不同目的: 创建新条目:显示“标题”和“文本”为空的表单 输入字段和提交按钮,将数据添加到数据库(之后 (验证) 编辑现有条目(将ID传递给构造函数):显示 带有“标题”和“文本”输入字段的表单,输入字段中填充检索到的数据 从DB和提交按钮向数据库添加数据(在 (验证) 查看现有条目(将ID传递给构造函数):显示 填写不可编辑的“标题”和“文本”字段(即“h1”和“p”) 使用从数据库检索的数据 删除现有条目(将
- 创建新条目:显示“标题”和“文本”为空的表单 输入字段和提交按钮,将数据添加到数据库(之后 (验证)
- 编辑现有条目(将ID传递给构造函数):显示 带有“标题”和“文本”输入字段的表单,输入字段中填充检索到的数据 从DB和提交按钮向数据库添加数据(在 (验证)
- 查看现有条目(将ID传递给构造函数):显示 填写不可编辑的“标题”和“文本”字段(即“h1”和“p”) 使用从数据库检索的数据
- 删除现有条目(将ID传递给构造函数):显示 填写不可编辑的“标题”和“文本”字段(即“h1”和“p”) 具有从数据库检索的数据和用于从数据库中删除数据的提交按钮 数据库
你有什么建议吗?提前感谢。大多数PHP MVC都包含针对此类问题的良好设计模式。查看以下框架:
- 拉雷维尔:
- CakePHP:
- Zend框架:
如果您想保持简单,并且只为这个问题设计一个PHP类,那么您应该查看CRUD、ORM和ActiveRecord。以下链接可能会派上用场:大多数PHP MVC都包含针对此类问题的良好设计模式。查看以下框架:
- 拉雷维尔:
- CakePHP:
- Zend框架:
class ArticleMapper
{
protected $pdo;
protected $table = 'some_table';
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
/**
* @return boolean Depending on success
*/
public function insert($id, $title, $text)
{
$query = sprintf('INSERT INTO `table` (`id`, `title`, `text`) VALUES (:id, :title, :text)', $this->table);
$stmt = $this->pdo->prepare($query);
return $stmt->execute(array(
':title' => $title,
':text' => $text,
':id' => $id,
));
}
public function fetchById($id)
{
$query = sprintf('SELECT * FROM `%s` WHERE `id` =:id');
$stmt = $this->pdo->prepare($query);
$stmt->execute(array(
':id' => $id
));
return $stmt->fetch();
}
// ... The rest for deleteById() and updateById()
}
福利?
好吧,如果它们不明显,那么听它们说是有意义的:
- 将应用程序逻辑与其存储逻辑分离李>
- 很快
- 从一个数据库供应商切换到另一个数据库供应商很容易
class Foo
{
public function __construct(MapperInterface $mapper)
{
$this->mapper = $mapper;
}
public function doSomeComputationsWithId($id)
{
$data = $this->mapper->fetchById($id);
// .. do computations here
return $result;
}
}
$mapper = new Article_MySQL_Mapper($pdo);
// or
$mapper = new Article_MongoDB_Mapper($mongoInstance);
$foo = new Foo($mapper);
print_r($foo->doSomeComputationsWithId($_GET['id']));
你实际上是在找一个新的工作。
由于所有这些CRUD“操作”都属于相同的职责(它们进行抽象表访问),因此不需要为每个操作实现单独的类。您将实现一个方法来抽象与您正在使用的表相关的单个操作。您可以这样做,例如:
class ArticleMapper
{
protected $pdo;
protected $table = 'some_table';
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
/**
* @return boolean Depending on success
*/
public function insert($id, $title, $text)
{
$query = sprintf('INSERT INTO `table` (`id`, `title`, `text`) VALUES (:id, :title, :text)', $this->table);
$stmt = $this->pdo->prepare($query);
return $stmt->execute(array(
':title' => $title,
':text' => $text,
':id' => $id,
));
}
public function fetchById($id)
{
$query = sprintf('SELECT * FROM `%s` WHERE `id` =:id');
$stmt = $this->pdo->prepare($query);
$stmt->execute(array(
':id' => $id
));
return $stmt->fetch();
}
// ... The rest for deleteById() and updateById()
}
福利?
好吧,如果它们不明显,那么听它们说是有意义的:
- 将应用程序逻辑与其存储逻辑分离李>
- 很快
- 从一个数据库供应商切换到另一个数据库供应商很容易
class Foo
{
public function __construct(MapperInterface $mapper)
{
$this->mapper = $mapper;
}
public function doSomeComputationsWithId($id)
{
$data = $this->mapper->fetchById($id);
// .. do computations here
return $result;
}
}
$mapper = new Article_MySQL_Mapper($pdo);
// or
$mapper = new Article_MongoDB_Mapper($mongoInstance);
$foo = new Foo($mapper);
print_r($foo->doSomeComputationsWithId($_GET['id']));
CRUD,数据表网关,数据行网关,活动记录,有很多。CRUD,数据表网关,数据行网关,活动记录,有很多。