【问题标题】:Joomla: Can't seem to drop or create tables or even delete rows using JDatabaseJoomla:似乎无法使用 JDatabase 删除或创建表甚至删除行
【发布时间】:2012-04-01 19:34:58
【问题描述】:

这是我在 phpmyadmin 中完成的一个更大查询的子集,但我对让它在我在 Joomla 中构建的组件中工作的语法有点迷茫和困惑。

在 phpmyadmin 中工作的代码:

drop table if exists tests_mod_ques;
create table tests_mod_ques as
select p.student_userid, q.question_mod, q.question_id, p.student_passedchk

from `tests_students` p 
inner join `tests_questions` q on p.question_id = q.question_id
group by  p.student_userid, q.question_mod, q.question_id, p.student_passedchk
;

引发 1064 语法错误的代码:

$query = parent::getListQuery(); 

$query->dropTable('#__tests_mod_ques', 'true');
$query->createTable('#__tests_mod_ques AS 
    select (p.student_userid
    , q.question_mod
    , q.question_id
    , p.student_passedchk)
    ');
$query->from('#__tests_students AS p');
$query->join('#__tests_questions AS q ON p.question_id = q.question_id');
$query->group('p.student_userid
    , q.question_mod
    , q.question_id
    , p.student_passedchk
    '); 

  ....  
    $db = $this->getDbo();
    return $query;  

我只测试了 dropTable 语句,即使它没有抛出错误,它也没有删除表,也没有显示在调试器中,所以我可能没有正确使用它: http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#dropTable

似乎找不到任何关于创建的参考,除了这个: http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getTableCreate

我还有一个 INSERT…ON DUPLICATE KEY UPDATE 查询即将出现,我也不知道如何处理。

谢谢!

编辑

我尝试删除行作为解决方法,但也没有注册。再次检查调试器,删除查询没有出现。

$query->delete('#__tests_mod_ques');

这应该根据http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#$deletehttp://docs.joomla.org/JDatabaseQuery::delete/1.6 工作

令人沮丧!有人知道吗?

谢谢!

【问题讨论】:

  • 能否打印查询,以便查看它是否被 DB 类正确解释?
  • 如果我做得对(这可能是一个大问题!)然后 dropTable 不会出现。在那里扔了一个print_r($query);,dropTable 没有出现,但查询的其余部分(正在工作的... 部分)出现了。它也没有出现在调试器中,为什么我认为我没有正确使用它但找不到好的例子。

标签: php joomla joomla1.7 joomla1.6 joomla2.5


【解决方案1】:

您可以尝试以下方法 -

$query->createTable('#__tests_mod_ques AS 
select (p.student_userid
, q.question_mod
, q.question_id
, p.student_passedchk)
from #__tests_students AS p
inner join #__tests_questions AS q ON p.question_id = q.question_id
group by p.student_userid
, q.question_mod
, q.question_id
, p.student_passedchk');

【讨论】:

    【解决方案2】:

    问题应该在于 $query 属于 JDatabaseQuery 类,因为你初始化它的方式,所以它只有这里列出的方法:http://docs.joomla.org/JDatabaseQuery/1.6

    您尝试调用的方法实际上是 JDatabase 的方法,您在最后创建了一个实例。因此,您应该能够执行 $db->createTable(... etc 并且它会实际运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多