PHP 通用数据库访问
我希望我的 PHP Zend 应用程序能够访问数据库。
因为任务只有两种类型:从数据库获取和设置值,所以我考虑使用通用方法来简化它。
它可能看起来像这个例子。
namespace MyModule\Model;
use Zend\Db\Table\AbstractTable;
class MyTable extends AbstractTable
{
protected $_name = 'tablename';
public function getRow($selection)
{
$output = array();
foreach($selection as $key => $value)
{
$row = $this->fetchRow($key ' = ' . $value);
if (!$row) throw new Exception("error");
array_merge($output, $row->toArray());
}
return $output;
}
public function addRow($values)
{
$this->insert($values);
}
public function updateRow($selection, $values)
{
foreach($selection as $key => $value)
{
$this->update($values, $key ' = ' . $value);
}
}
public function deleteRow($selection)
{
foreach($selection as $key => $value)
{
$this->delete($key ' = ' . $value);
}
}
}
是否有任何安全或设计方面的理由反对这种方法?我考虑过让它们全局访问数据库,就像
$row = database('mydatabase')->table('mytable')->getRow(array('id'=>'5'));
这个解决方案将取代所有简单的数据库模型。
I want my PHP Zend Application to get access to the database.
Because the tasks are only two types: Get and Set a value from the database, I thought about simplifying it with universal methods.
It could look like this example.
namespace MyModule\Model;
use Zend\Db\Table\AbstractTable;
class MyTable extends AbstractTable
{
protected $_name = 'tablename';
public function getRow($selection)
{
$output = array();
foreach($selection as $key => $value)
{
$row = $this->fetchRow($key ' = ' . $value);
if (!$row) throw new Exception("error");
array_merge($output, $row->toArray());
}
return $output;
}
public function addRow($values)
{
$this->insert($values);
}
public function updateRow($selection, $values)
{
foreach($selection as $key => $value)
{
$this->update($values, $key ' = ' . $value);
}
}
public function deleteRow($selection)
{
foreach($selection as $key => $value)
{
$this->delete($key ' = ' . $value);
}
}
}
Are there any security or design arguments against this methods? I thought about making them global to access database like
$row = database('mydatabase')->table('mytable')->getRow(array('id'=>'5'));
This solution would replace all the simple database models.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我将提供一个有关 Zend_Db 如何工作的简单示例,然后您可以演示您的意图,我不确定我是否完全理解。
模型
和控制器
通过这个简单的模型,您的所有 CRUD 功能都已可用于您的模型和控制器。
我们创建新方法或覆盖现有方法,以提供对业务逻辑的更多控制。
我们大多数人都需要比简单的 CRUD 功能更多的功能。
例如,我有一个删除功能,需要检查其他数据才能成功:
我希望这可以使我们的讨论更加清晰。
I think I'll provide a simple example of how Zend_Db works, then you can demonstrate what you intend I'm not sure I completely understand.
The Model
And the controller
with just this simple model all of your CRUD functions are already available to your models and controllers.
We create new methods or override existing methods to provide more control over our business logic.
Most of us have need for more then simple CRUD functions.
For example I have a delete function that requires a check for other data to be successful:
I hope this adds some clarity to our discussion.