从数组填充后保存行 - Zend

发布于 2024-08-07 21:12:27 字数 1111 浏览 10 评论 0原文

又是一个 Zend Framework 问题!任何帮助表示赞赏。

我有一个数据库表类,我想在发布表单时用它来插入/更新行,并且我想使用 save() 方法,这样它就可以通过相同的方法。


//Users Db Table class
class DBTables_Users extends Zend_Db_Table_Abstract
{
    protected $_name = "users";
    protected $_primary = "userID";
}

所以这是我的测试代码......


$userArray = array( 'userID' => 1,
      'firstName' => 'Test',
      'lastName' => 'Test',
      'emailAddress' => '[email protected]'
      );

$user = new DBTables_Users();

$newUserRow = $user->createRow($userArray);

$newUserRow->save();

这在没有“userID”项的情况下工作得很好并且插入得很好,但是如果我添加它,它会尝试再次插入它并出现重复键错误的错误。

对我来说,该行应该确认主键的存在,并将其用作更新该行的 where 子句。然而,在深入研究代码之后,它检查的唯一一件事是确定是否插入或更新,是否存在“_cleanData”变量,如果配置具有“数据”选项,则在构造时填充该变量,但在使用时没有“数据”选项创建行方法。

我是否以错误的方式处理这个问题,或者是否需要在覆盖中自己设置 _cleanData 属性来进行丑陋的修复?

干杯 斯图尔特

Once again a Zend Framework question! Any help is appreciated.

I have a DB table class which i want to use to insert/update rows when a form is posted, and i would like to use the save() method so it can all go through the same method.


//Users Db Table class
class DBTables_Users extends Zend_Db_Table_Abstract
{
    protected $_name = "users";
    protected $_primary = "userID";
}

So here is my test code...


$userArray = array( 'userID' => 1,
      'firstName' => 'Test',
      'lastName' => 'Test',
      'emailAddress' => '[email protected]'
      );

$user = new DBTables_Users();

$newUserRow = $user->createRow($userArray);

$newUserRow->save();

This works perfectly without the "userID" item and inserts fine, but if i add that in it tries to insert it again and errors with a duplicate key error.

To me the row should acknowledge the presence of the primary key and use it as the where clause to update the row. However after delving into the code the only thing that it checks, to determine whether its an insert or update, is the presence of the "_cleanData" variable which is populated on construction if the config has a "data" option which it doesnt when using the createRow method.

Am i going about this the wrong way or does it need an ugly fix of setting the _cleanData property myself in an override?

Cheers
Stuart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

黑寡妇 2024-08-14 21:12:27

如果条目是新条目,您肯定不会从表单中传递 ID 吗?

你不能快速检查一下它是否为空或空,然后调用插入而不是更新吗?

不要尝试用一种方法做太多事情,如果他们执行不同的任务,请将它们分开。

下面是我的插入/更新方法的示例,该方法位于扩展 Zend_Db_Table 的类中(Zend Framework 1.8)

public function insertQuote($quote)
    {
        $data = array('id'=>null,'quote'=>$quote, 'dateCreated'=>NOW());
        $id = $this->insert($data);

        return $id;
    }

public function updateQuote($id, $quote)
    {
        $data = array(
        'quote'=>$quote
        );
        $this->update($data, 'id = ' . (int)$id);
    }

Surely if the entry is a new one you wont have an ID being passed from your form?

Can't you do a quick check if that is null or empty then call an insert rather than the update?

Don't try and do too many things with one method, if they do a different task seperate them out.

Heres an example of my insert / update methods situated in a class which extends Zend_Db_Table (Zend Framework 1.8)

public function insertQuote($quote)
    {
        $data = array('id'=>null,'quote'=>$quote, 'dateCreated'=>NOW());
        $id = $this->insert($data);

        return $id;
    }

public function updateQuote($id, $quote)
    {
        $data = array(
        'quote'=>$quote
        );
        $this->update($data, 'id = ' . (int)$id);
    }
梦归所梦 2024-08-14 21:12:27

这样的更改将导致无法插入具有用户指定的主键(例如,从序列中预取的自然主键)的行。它不能仅仅基于数组包含主键这一事实来假设表已经包含该行。

您可以编写一个与 createRow 类似的新函数,它使用 'stored' => 构造行实例。正确。

Such a change would make it impossible to insert a row with user-specified primary key (e.g. natural primary keys, pre-fetched from a sequence). It can't assume the table already contains the row, just based on the fact that the array contains the primary key.

You can write a new function, similar to createRow, that constructs the row instance with 'stored' => true.

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