【发布时间】:2019-06-16 11:25:06
【问题描述】:
切入正题。
我需要先更新数据库中的一个字段,使用该字段计算新值。
例如字段:https://i.stack.imgur.com/FADH6.jpg
现在我正在使用 Joomla updateObject 函数。我的目标是在不使用 select 语句的情况下从数据库表中获取“花费”值。
然后我需要用它计算一个新值,例如 (spent + 10.00) 并用新值更新该字段。查看下面的代码:
// Create an object for the record we are going to update.
$object = new stdClass();
// Must be a valid primary key value.
$object->catid = $item['category'];
$object->spent = ($object->spent - $item['total']);
// Update their details in the users table using id as the primary key.
$result = JFactory::getDbo()->updateObject('#__mytable', $object, 'catid');
我需要计算的位是
$object->spent = ($object->spent - $item['total']);
我意识到我可以使用单独的插入语句,但我想知道是否有更好的方法。非常感谢任何帮助。
它需要像这样工作,没有选择(工作示例)
$query = $db->getQuery(true);
$query->select($db->quoteName('spent'));
$query->from($db->quoteName('#__mytable'));
$query->where($db->quoteName('catid')." = ". $item['category']);
// Reset the query using our newly populated query object.
$db->setQuery($query);
$oldspent = $db->loadResult();
// Create an object for the record we are going to update.
$object = new stdClass();
// Must be a valid primary key value.
$object->catid = $item['category'];
$object->spent = ($oldspent - $item['total']);
// Update their details in the users table using id as the primary key.
$result = JFactory::getDbo()->updateObject('#__mytable', $object, 'catid');
【问题讨论】:
-
对于Joomla Stack Exchange 来说似乎是个好问题。
-
而不是提供数据库架构的屏幕截图。请使用来自 phpMyAdmin 的实际 EXPORT 结构和数据为我们模拟一个 sqlfiddle。然后根据您的样本数据告诉我们您想要的确切结果。通过提供上下文,这使您的问题变得非常清晰,并且在我看来“值得投票”。
-
@mickmacusa 拥有一个 sqlfiddle 页面会很有帮助,但我没有时间构建一个。屏幕截图显示了需要更新的“花费”列。它只需要获取那里的值并在对其进行计算后用新值更新它。非常直接。
-
meta.stackoverflow.com/q/333952/2943403 在 phpMyAdmin 中使用 EXPORT 选项卡将花费您大约 2 分钟(包括登录)“输出为文本”,然后将 CREATE 和 INSERT 查询粘贴到演示中,然后发布链接。
标签: model-view-controller joomla joomla3.0 joomla-extensions