【问题标题】:Update a database field with Joomla UpdateObject method with a calculated field from same table使用 Joomla UpdateObject 方法使用来自同一表的计算字段更新数据库字段
【发布时间】: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


【解决方案1】:

尝试使用updateObject('#__mytable', $object, 'catid'); 的症结在于您的查询逻辑需要在计算中引用列名以将“差异”分配为新值。用值减去另一个值来更新列值的原始 mysql 查询语法如下:

"`spent` = `spent` - {$item['total']}"

updateObject() 会将spent - {$item['total']} 转换为文字字符串,数据库将需要一个数值,因此 UPDATE 会记录一个0 值。换句话说,$db->getAffectedRows() 会给你一个正数并且不会产生错误,但是你没有得到想要的数学运算。

解决方法是放弃updateObject() 作为工具并构建不带对象的UPDATE 查询——不用担心它不会太复杂。我将构建一些诊断和故障检查,但你可以删除任何你想要的部分。

我已经在我的本地主机上测试了以下代码:

$db = JFactory::getDBO();
try {
    $query = $db->getQuery(true)
                ->update($db->quoteName('#__mytable'))
                ->set($db->quoteName("price") . " = " . $db->qn("price") . " - " . (int)$item['total'])
                ->where($db->quoteName("catid") . " = " . (int)$item['category']);
    echo $query->dump();  // see the generated query (but don't show to public)
    $db->setQuery($query);
    $db->execute();
    if ($affrows = $db->getAffectedRows()) {
        JFactory::getApplication()->enqueueMessage("Updated. Affected Rows: $affrows", 'success');
    } else {
        JFactory::getApplication()->enqueueMessage("Logic Error", 'error');
    }
} catch (Exception $e) {
    JFactory::getApplication()->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');  // never show getMessage() to public
}

这里是讨论 mysql 减法逻辑的 StackOverflow 页面:update a column by subtracting a value

【讨论】:

  • 这个选项可以正常工作。但不完全是我想要的。不确定我所问的是否可以使用 updateObject 来完成。
  • 我试图解释如何使用 updateObject 将对象键指定为列名,将值指定为列值。因为您的 sql 逻辑需要新的 price 值是列名与文字字符串的混合,所以 updateObject 不适合该任务(AFAIK)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多