Zend 数据库适配器 - 复杂的 MySQL 查询
我在导航模型中定义了一个执行查询的函数,我想知道是否有更“Zendy”的方式来生成/执行查询。我正在使用的查询是由 Bill Karwin 在 这里的另一个线程用于设置任意记录顺序。我尝试使用准备好的语句,但 SIGN() 函数中的值被引用。
我正在使用 MySQL 的 PDO 适配器。
/**
*
*/
public function setPosition($parentId, $oldPosition, $newPosition)
{
$parentId = intval($parentId);
$oldPosition = intval($oldPosition);
$newPosition = intval($newPosition);
$this->getAdapter()->query("
UPDATE `navigation`
SET `position` = CASE `position`
WHEN $oldPosition THEN $newPosition
ELSE `position` + SIGN($oldPosition - $newPosition)
END
WHERE `parent_id` = $parentId
AND `position` BETWEEN LEAST($oldPosition, $newPosition)
AND GREATEST($oldPosition, $newPosition)
");
return $this;
}
I have defined a function in my Navigation model that executes a query, and I was wondering if there's a more "Zendy" way of generating/executing the query. The query I'm using was proposed by Bill Karwin on another thread here for setting arbitrary record order. I tried using a prepared statement, but the values in the SIGN() function got quoted.
I'm using the PDO adapter for MySQL.
/**
*
*/
public function setPosition($parentId, $oldPosition, $newPosition)
{
$parentId = intval($parentId);
$oldPosition = intval($oldPosition);
$newPosition = intval($newPosition);
$this->getAdapter()->query("
UPDATE `navigation`
SET `position` = CASE `position`
WHEN $oldPosition THEN $newPosition
ELSE `position` + SIGN($oldPosition - $newPosition)
END
WHERE `parent_id` = $parentId
AND `position` BETWEEN LEAST($oldPosition, $newPosition)
AND GREATEST($oldPosition, $newPosition)
");
return $this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Zend_Db_Select
和/或Zend_Db_Expr
,但是如果它按原样工作,请不要更改它。实际上没有必要仅仅因为任何 ZF 组件的存在或让您的代码更加Zendy而使用它们。使用它们来解决特定问题。请记住,每个抽象都会使您的代码变慢一些。可能不多,但也可能没有必要。我可以根据我自己在一个项目中的经验来谈谈,在该项目中,我们不得不使用尽可能多的采埃孚组件,尽管我们本来可以不用更简单的组件来完成。没有得到回报,我们发现自己后来重构了很多。
You could use
Zend_Db_Select
and/orZend_Db_Expr
, but if it works like it is, don't change it. There is really no need to use any of the ZF components just because they exist or to make your code more Zendy. Use them to solve a specific problem.Keep in mind that every abstraction will make your code some degrees slower. Might not be much, but also might not be necessary. I can speak from my own experience from a project where we succumbed to use as many ZF components as possible, even though we could have done without and simpler. Didn't pay off and we found ourselves refactoring out a lot of them later.