在 Zend 中使用 IN 运算符的“更新语句”不正确

发布于 2024-12-13 02:19:01 字数 674 浏览 0 评论 0原文

我有一个函数想要执行如下所示的语句:

UPDATE coupon_users SET status = status | '1' WHERE id IN ('3','4')

在 coupon_users 模型中,我编写了如下方法:

/**
* @param array $ids        #array(3,4)
* @param array $status     #1
*/
public function updateStatus(array $ids, $status)
{
    $result = $this->_db->query(
        "UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
        array(
            $status,
            $ids
        )
    )->execute();
    return $result;
}

但查询始终是:

UPDATE coupon_users SET status = status | '1' WHERE id IN ('Array')

我不知道我错了什么在此,请帮助我,非常感谢。

I have a function which is wanted to execute a statement like below:

UPDATE coupon_users SET status = status | '1' WHERE id IN ('3','4')

And in coupon_users model, I wrote a method like below do to:

/**
* @param array $ids        #array(3,4)
* @param array $status     #1
*/
public function updateStatus(array $ids, $status)
{
    $result = $this->_db->query(
        "UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
        array(
            $status,
            $ids
        )
    )->execute();
    return $result;
}

But the query is always:

UPDATE coupon_users SET status = status | '1' WHERE id IN ('Array')

I don't know what am I wrong here, please help me, many thanks.

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

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

发布评论

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

评论(4

最偏执的依靠 2024-12-20 02:19:01

根据PDO文档(Zend_Db使用PDO作为其数据库访问后端) :

您不能将多个值绑定到单个命名参数,例如
例如,SQL 语句的 IN() 子句。

因此,您可能需要进一步准备查询,以便它包含与数组中的元素一样多的标记。可能的解决方案如下:

// Compose the query
$queryToExecute = "UPDATE {$this->_name} SET status = status | ? WHERE id IN (";
$questionMarks = array();
for ($id in $ids) {
    $questionMarks[] = '?';
}
$queryToExecute .= implode(',', $questionMarks);
$queryToExecute .= ')';

// $queryToExecute should have the format "UPDATE ... WHERE id IN (?,?,?,...?)"

// Execute it
$result = $this->_db->query(
    $queryToExecute,
    array($status, $ids)
)->execute();

希望有所帮助,

According to the PDO documentation (Zend_Db uses PDO as its DB access backend):

You cannot bind multiple values to a single named parameter in, for
example, the IN() clause of an SQL statement.

So, you'll probably need to prepare a bit further your query, so that it contains as many markers as elements in the array. A possible solution could be the following:

// Compose the query
$queryToExecute = "UPDATE {$this->_name} SET status = status | ? WHERE id IN (";
$questionMarks = array();
for ($id in $ids) {
    $questionMarks[] = '?';
}
$queryToExecute .= implode(',', $questionMarks);
$queryToExecute .= ')';

// $queryToExecute should have the format "UPDATE ... WHERE id IN (?,?,?,...?)"

// Execute it
$result = $this->_db->query(
    $queryToExecute,
    array($status, $ids)
)->execute();

Hope that helps,

深爱成瘾 2024-12-20 02:19:01

尝试:

public function updateStatus(array $ids, $status)
{
    $result = $this->_db->query(
        "UPDATE {$this->_name} SET status = ? WHERE id IN (?)",
        array(
            $status,
            implode(',',$ids)
        )
    )->execute();
    return $result;
}

更新:

你尝试过吗?:

$this->_db->update($this->_name, array('status'=>$status), array('id IN (?)'=>$ids));

的实例

我还没有测试过,它还取决于 $this->_db 是http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update

try:

public function updateStatus(array $ids, $status)
{
    $result = $this->_db->query(
        "UPDATE {$this->_name} SET status = ? WHERE id IN (?)",
        array(
            $status,
            implode(',',$ids)
        )
    )->execute();
    return $result;
}

Update:

Have you tried?:

$this->_db->update($this->_name, array('status'=>$status), array('id IN (?)'=>$ids));

I haven't tested it, it also depends on what $this->_db is an instance of

http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update

爱情眠于流年 2024-12-20 02:19:01

试试这个..

public function updateStatus(array $ids, $status)
{
    $inarray=   implode(',',$ids);
    $result = $this->_db->query(
        "UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
        array(
            $status, 
            $inarray
        )
    )->execute();
    return $result;
}

Try this..

public function updateStatus(array $ids, $status)
{
    $inarray=   implode(',',$ids);
    $result = $this->_db->query(
        "UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
        array(
            $status, 
            $inarray
        )
    )->execute();
    return $result;
}
爺獨霸怡葒院 2024-12-20 02:19:01

它对我来说工作得很好。

     $existingImagesIds = array(1, 2, 3, 7);

     $where = $pImgModel->getAdapter()->quoteInto("id in (?) ", $existingImagesIds);

     $pImgModel->update(array('status' => '0'), $where);

Its working fine for me.

     $existingImagesIds = array(1, 2, 3, 7);

     $where = $pImgModel->getAdapter()->quoteInto("id in (?) ", $existingImagesIds);

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