Zend_Db - 构建删除查询

发布于 2024-10-20 07:45:49 字数 469 浏览 4 评论 0原文

我正在尝试在 Zend Framework 中重新创建以下内容,但不知道该怎么做:

DELETE FROM mytablename WHERE date( `time_cre` ) < curdate( ) - INTERVAL 4 DAY 

我在想:

$table = $this->getTable();
$db = Zend_Registry::get('dbAdapter');
$db->delete($table, array(
    'date(`time_cre`) < curdate() - interval 4'
));

这看起来正确吗?

处理这样的事情最好的方法是什么?


编辑: 嗯!抱歉,我正在从用于测试的 SELECT 中进行调整,并且在粘贴它时没有正确更改语法。我已经编辑了示例来修复它。

I'm trying to recreate the following in Zend Framework and am not sure how to do it:

DELETE FROM mytablename WHERE date( `time_cre` ) < curdate( ) - INTERVAL 4 DAY 

I was thinking something like:

$table = $this->getTable();
$db = Zend_Registry::get('dbAdapter');
$db->delete($table, array(
    'date(`time_cre`) < curdate() - interval 4'
));

Does that seem correct?

What is the best way to handle something like this?


EDIT: Ack! Sorry, I was adapting this from a SELECT I was using to test and didn't change the syntax properly when I pasted it in. I've edited the example to fix it.

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

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

发布评论

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

评论(3

穿透光 2024-10-27 07:45:49

弄清楚了...

public function pruneOld($days) {
    $table = $this->getTable();
    $db = Zend_Registry::get('dbAdapter');
    $where = $db->quoteInto("DATE(`time_cre`) < CURDATE() - INTERVAL ? DAY", $days);

    return $table->delete($where);
}

$table 获取我想要编辑的表的引用...

$db 获取数据库适配器的实例,以便我可以使用 quoteInto()...

$where 构建接受 $days 的查询的主要部分,以使事情更加灵活。

创建一个操作来调用此方法...类似于:

public function pruneoldAction() {

    // Disable the view/layout stuff as I don't need it for this action
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    // Get the data model with a convenience method
    $data = $this->_getDataModel();

    // Prune the old entries, passing in the number of days I want to be older than
    $data->pruneOld(2);
}

现在点击:http://myhost/thiscontroller/pruneold/ 将删除条目。当然,任何点击该网址的人都会删除这些条目,但我已经采取了示例中未包含的步骤来处理此问题。希望这对某人有帮助。

Figured it out...

public function pruneOld($days) {
    $table = $this->getTable();
    $db = Zend_Registry::get('dbAdapter');
    $where = $db->quoteInto("DATE(`time_cre`) < CURDATE() - INTERVAL ? DAY", $days);

    return $table->delete($where);
}

$table gets an reference of the table I want to edit...

$db grabs an instance of the database adapter so I can use quoteInto()...

$where builds the main part of the query accepting $days to make things a bit more flexible.

Create an action to call this method... something like:

public function pruneoldAction() {

    // Disable the view/layout stuff as I don't need it for this action
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    // Get the data model with a convenience method
    $data = $this->_getDataModel();

    // Prune the old entries, passing in the number of days I want to be older than
    $data->pruneOld(2);
}

And now hitting: http://myhost/thiscontroller/pruneold/ will delete the entries. Of course, anyone hitting that url will delete the entries but I've taken steps not included in my example to deal with this. Hope this helps someone.

朦胧时间 2024-10-27 07:45:49

我通常这样做是为了删除一行

$table = new yourDB;
$row = $table->fetchRow($table->select()->where(some where params));

$row->delete();

I usually do this to delete a row

$table = new yourDB;
$row = $table->fetchRow($table->select()->where(some where params));

$row->delete();
枕花眠 2024-10-27 07:45:49

尝试:
$db->删除($表, 数组(
'日期(time_cre)< ? => new Zend_Db_Expr('curdate() - 间隔 4')
));

try:
$db->delete($table, array(
'date(time_cre) < ?' => new Zend_Db_Expr('curdate() - interval 4')
));

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