Model view controller 使用Joomla UpdateObject方法使用同一表中的计算字段更新数据库字段

Model view controller 使用Joomla UpdateObject方法使用同一表中的计算字段更新数据库字段,model-view-controller,joomla,joomla3.0,joomla-extensions,Model View Controller,Joomla,Joomla3.0,Joomla Extensions,切中要害 我需要更新数据库中的一个字段,首先使用该字段计算新值 例如,字段的类型: 现在我正在使用Joomla updateObject函数。我的目标是在不使用select语句的情况下从DB表中获取花费的值 然后我需要用它计算一个新值,比如花费+10.00,并用新值更新字段。查看下面的代码: // Create an object for the record we are going to update. $object = new stdClass(); // Must be a vali

切中要害

我需要更新数据库中的一个字段,首先使用该字段计算新值

例如,字段的类型:

现在我正在使用Joomla updateObject函数。我的目标是在不使用select语句的情况下从DB表中获取花费的值

然后我需要用它计算一个新值,比如花费+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']);
我意识到我可以使用单独的insert语句,但我想知道是否有更好的方法。非常感谢您的帮助

它需要这样工作,而不需要选择工作示例

$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');  

尝试使用updateObject“uuuMyTable”和$object“catid”的症结所在;您的查询逻辑需要在计算中引用列名,以将差异指定为新值。使用值减去另一个值来更新列值的原始mysql查询语法如下所示:

"`spent` = `spent` - {$item['total']}"
updateObject将花费的-{$item['total']}转换为文字字符串,数据库将需要一个数字值,因此更新将导致记录一个0值。换句话说,$db->getAffectedRows将为您提供一个正计数,并且不会生成任何错误,但您不会得到所需的数学操作

解决方法是放弃updateObject作为工具,构建一个没有对象的更新查询——不用担心,它不会太复杂。我将内置一些诊断和故障检查,但您可以删除任何您想要的部分

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

$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
}

下面是一个StackOverflow页面,讨论mysql减法逻辑:

对于堆栈交换来说似乎是个好问题。而不是提供数据库模式的屏幕截图。请为我们模拟一个sqlfiddle,并提供来自phpMyAdmin的实际导出结构和数据。然后根据您的样本数据告诉我们您的确切期望结果。通过提供上下文,这使您的问题非常清楚,在我看来,这是值得支持的。@mickmacusa拥有一个SQLFIDLE页面可能会有所帮助,但我没有时间构建一个页面。屏幕截图显示了需要更新的专栏。它只需要获取存在的值,并在对其进行计算后使用新值对其进行更新。很直截了当。使用phpMyAdmin中的导出选项卡将花费您大约2分钟的时间,包括登录以文本形式输出,然后将创建和插入查询粘贴到演示中,然后发布链接。此选项工作正常。但不是我想要的那样。我不确定是否可以使用updateObject完成我的请求。我试图解释如何使用updateObject将对象键指定为列名,将值指定为列值。因为sql逻辑需要新的price值是列名和文本字符串的混合,所以任务AFAIK没有配备updateObject。