在 Zend_Db_Table_Row::save() 上应用 SQL 函数

发布于 2024-11-24 19:24:15 字数 373 浏览 2 评论 0原文

保存 Zend_Db_Table_Row 时是否可以使 ZF 对一列应用 SQL 函数?

例如,如果 $row->save() 默认生成此 SQL 查询:

UPDATE table SET field = ? WHERE id = ?;

我希望它自动在此字段上应用 GeomFromText() 函数:

UPDATE table SET field = GeomFromText(?) WHERE id = ?;

谢谢有关如何使用 Zend_Db 执行此操作的任何提示!

Is it possible, when saving a Zend_Db_Table_Row, to make ZF apply a SQL function on one column?

For example, if $row->save() generates by default this SQL query:

UPDATE table SET field = ? WHERE id = ?;

I would like it to automatically apply the GeomFromText() function on this field:

UPDATE table SET field = GeomFromText(?) WHERE id = ?;

Thanks for any hint on how to do this with Zend_Db!

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

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

发布评论

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

评论(2

美人骨 2024-12-01 19:24:15

在您的类中定义一个继承自 Zend_Db_Table(而不是来自 Zend_Db_Table_Row)的自定义 update 方法,并使用 Zend_Db_Expr将列设置为函数返回值。

请参阅此处的文档: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.extending.insert-update

Define a custom update method in your class that inherits from Zend_Db_Table (not from the Zend_Db_Table_Row) and use a Zend_Db_Expr to set the column to the function return value.

See the docs here: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.extending.insert-update.

疑心病 2024-12-01 19:24:15

我只是猜测,但你可以尝试这个:

<?php
class MyTable extends Zend_Db_Table_Abstract 
{
  protected $_name = 'my_table';

  public function update(array $data, $where) {
       /**
         * Build "col = ?" pairs for the statement,
         * except for Zend_Db_Expr which is treated literally.
         */
        $set = array();
        $i = 0;
        foreach ($data as $col => $val) {
            if ($val instanceof Zend_Db_Expr) {
                $val = $val->__toString();
                unset($data[$col]);
            } else {
                if ($this->_db->supportsParameters('positional')) {
                    $val = ($col == 'field') ? 'GeomFromText(?)' : '?';
                } else {
                    if ($this->_db->supportsParameters('named')) {
                        unset($data[$col]);
                        $data[':col'.$i] = $val;
                        $val = ($col == 'field') ? 'GeomFromText(:col'.$i.')' : ':col'.$i;
                        $i++;
                    } else {
                        /** @see Zend_Db_Adapter_Exception */
                        require_once 'Zend/Db/Adapter/Exception.php';
                        throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
                    }
                }
            }
            $set[] = $this->_db->quoteIdentifier($col, true) . ' = ' . $val;
        }

        $where = $this->_whereExpr($where);

        /**
         * Build the UPDATE statement
         */
        $sql = "UPDATE "
             . $this->_db->quoteIdentifier($this->_name , true)
             . ' SET ' . implode(', ', $set)
             . (($where) ? " WHERE $where" : '');

        /**
         * Execute the statement and return the number of affected rows
         */
        if ($this->_db->supportsParameters('positional')) {
            $stmt = $this->_db->query($sql, array_values($data));
        } else {
            $stmt = $this->_db->query($sql, $data);
        }
        $result = $stmt->rowCount();
        return $result;
    }

    protected function _whereExpr($where)
    {
        if (empty($where)) {
            return $where;
        }
        if (!is_array($where)) {
            $where = array($where);
        }
        foreach ($where as $cond => &$term) {
            // is $cond an int? (i.e. Not a condition)
            if (is_int($cond)) {
                // $term is the full condition
                if ($term instanceof Zend_Db_Expr) {
                    $term = $term->__toString();
                }
            } else {
                // $cond is the condition with placeholder,
                // and $term is quoted into the condition
                $term = $this->quoteInto($cond, $term);
            }
            $term = '(' . $term . ')';
        }

        $where = implode(' AND ', $where);
        return $where;
    }
}
?>

I am just guessing but you could try this:

<?php
class MyTable extends Zend_Db_Table_Abstract 
{
  protected $_name = 'my_table';

  public function update(array $data, $where) {
       /**
         * Build "col = ?" pairs for the statement,
         * except for Zend_Db_Expr which is treated literally.
         */
        $set = array();
        $i = 0;
        foreach ($data as $col => $val) {
            if ($val instanceof Zend_Db_Expr) {
                $val = $val->__toString();
                unset($data[$col]);
            } else {
                if ($this->_db->supportsParameters('positional')) {
                    $val = ($col == 'field') ? 'GeomFromText(?)' : '?';
                } else {
                    if ($this->_db->supportsParameters('named')) {
                        unset($data[$col]);
                        $data[':col'.$i] = $val;
                        $val = ($col == 'field') ? 'GeomFromText(:col'.$i.')' : ':col'.$i;
                        $i++;
                    } else {
                        /** @see Zend_Db_Adapter_Exception */
                        require_once 'Zend/Db/Adapter/Exception.php';
                        throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
                    }
                }
            }
            $set[] = $this->_db->quoteIdentifier($col, true) . ' = ' . $val;
        }

        $where = $this->_whereExpr($where);

        /**
         * Build the UPDATE statement
         */
        $sql = "UPDATE "
             . $this->_db->quoteIdentifier($this->_name , true)
             . ' SET ' . implode(', ', $set)
             . (($where) ? " WHERE $where" : '');

        /**
         * Execute the statement and return the number of affected rows
         */
        if ($this->_db->supportsParameters('positional')) {
            $stmt = $this->_db->query($sql, array_values($data));
        } else {
            $stmt = $this->_db->query($sql, $data);
        }
        $result = $stmt->rowCount();
        return $result;
    }

    protected function _whereExpr($where)
    {
        if (empty($where)) {
            return $where;
        }
        if (!is_array($where)) {
            $where = array($where);
        }
        foreach ($where as $cond => &$term) {
            // is $cond an int? (i.e. Not a condition)
            if (is_int($cond)) {
                // $term is the full condition
                if ($term instanceof Zend_Db_Expr) {
                    $term = $term->__toString();
                }
            } else {
                // $cond is the condition with placeholder,
                // and $term is quoted into the condition
                $term = $this->quoteInto($cond, $term);
            }
            $term = '(' . $term . ')';
        }

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