PHP 通用数据库访问

发布于 2024-12-28 20:49:04 字数 1243 浏览 0 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2025-01-04 20:49:04

我想我将提供一个有关 Zend_Db 如何工作的简单示例,然后您可以演示您的意图,我不确定我是否完全理解。
模型

// /application/Models/DbTable/MyTable.php
<?php
class Application_Model_DbTable_MyTable extends Zend_Db_Table_Abstract {

    //actual name of database table
    protected $_name = 'My_Table';
    //name of primary key
    protected $_primary = 'id';

}

和控制器

// /application/controllers/MyController.php
<?php
class Mycontroller extends Zend_Controller_Action {

    public function init() {
}

    public function indexAction() {
        //instantiate DbTable Model and execute FetchAll returns Rowset object
        //call ->toArray() for array to be returned: $model->fetchAll()->toArray();
        $model = new Application_Model_DbTable_MyTable();
        $model->fetchAll();
}

通过这个简单的模型,您的所有 CRUD 功能都已可用于您的模型和控制器。
我们创建新方法或覆盖现有方法,以提供对业务逻辑的更多控制。
我们大多数人都需要比简单的 CRUD 功能更多的功能。
例如,我有一个删除功能,需要检查其他数据才能成功:

// /application/Models/DbTable/Track.php
 public function deleteTrack($trackId) {

        $trackRowset = $this->find($trackId);
        $trackToGo = $trackRowset->current();

        $usedBy = $trackToGo->findDependentRowset('Application_Model_DbTable_Member');
        if (count($usedBy) == 0) {

            $where = $this->getAdapter()->quoteInto('trackid = ?', $trackId);

            $this->delete($where);
        } else {
            throw new Zend_Exception('Track is still assigned to member(s) and cannot be deleted.'
                . "<br />" . 'Members: ' . count($usedBy));
        }
    }

我希望这可以使我们的讨论更加清晰。

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

// /application/Models/DbTable/MyTable.php
<?php
class Application_Model_DbTable_MyTable extends Zend_Db_Table_Abstract {

    //actual name of database table
    protected $_name = 'My_Table';
    //name of primary key
    protected $_primary = 'id';

}

And the controller

// /application/controllers/MyController.php
<?php
class Mycontroller extends Zend_Controller_Action {

    public function init() {
}

    public function indexAction() {
        //instantiate DbTable Model and execute FetchAll returns Rowset object
        //call ->toArray() for array to be returned: $model->fetchAll()->toArray();
        $model = new Application_Model_DbTable_MyTable();
        $model->fetchAll();
}

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:

// /application/Models/DbTable/Track.php
 public function deleteTrack($trackId) {

        $trackRowset = $this->find($trackId);
        $trackToGo = $trackRowset->current();

        $usedBy = $trackToGo->findDependentRowset('Application_Model_DbTable_Member');
        if (count($usedBy) == 0) {

            $where = $this->getAdapter()->quoteInto('trackid = ?', $trackId);

            $this->delete($where);
        } else {
            throw new Zend_Exception('Track is still assigned to member(s) and cannot be deleted.'
                . "<br />" . 'Members: ' . count($usedBy));
        }
    }

I hope this adds some clarity to our discussion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文