简体   繁体   English

Joomla:似乎无法使用JDatabase删除或创建表甚至删除行

[英]Joomla: Can't seem to drop or create tables or even delete rows using JDatabase

This is a subset of a larger query that I worked out in phpmyadmin but I'm a bit lost and confused at the syntax of getting it to work in a component I'm building in Joomla. 这是我在phpmyadmin中解决的一个较大查询的子集,但是我对让它在我在Joomla中构建的组件中工作的语法有点迷茫和困惑。

The code that works in phpmyadmin: 在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
;

The code that's throwing a 1064 syntax error: 引发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;  

I tested just the dropTable statment and even though it didn't throw an error it didn't drop the table nor show up in the debugger so I'm probably not using that right: http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#dropTable 我只测试了dropTable语句,即使它没有引发错误,也没有删除表,也没有显示在调试器中,所以我可能没有正确使用它: http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#dropTable : http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#dropTable

Can't seem to find any reference to create except for this: http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getTableCreate 除此之外,似乎找不到要创建的任何引用: http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getTableCreate : http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getTableCreate

Also I have an INSERT…ON DUPLICATE KEY UPDATE query coming up that I'm not sure how to approach either. 我也有一个INSERT…ON DUPLICATE KEY UPDATE查询,我不确定该如何处理。

Thanks! 谢谢!

Edit 编辑

I've tried deleting rows as a workaround, also doesn't register. 我尝试删除行作为一种解决方法,也没有注册。 Again, checked the debugger and the delete query isn't showing up. 再次,检查调试器,并且不显示删除查询。

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

That should work according to http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#$delete and http://docs.joomla.org/JDatabaseQuery::delete/1.6 这应该根据http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#$deletehttp://docs.joomla.org/JDatabaseQuery::delete/1.6

Frustrating! 令人沮丧! Anyone have any idea? 有人知道吗

Thanks! 谢谢!

you can try the following - 您可以尝试以下-

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

The problem should be from the fact that $query is of the class JDatabaseQuery because of how you initialized it, so it only has the methods listed here: http://docs.joomla.org/JDatabaseQuery/1.6 问题可能是由于$ query由于初始化方式而属于JDatabaseQuery类,因此它仅具有此处列出的方法: http : //docs.joomla.org/JDatabaseQuery/1.6

The methods that you are trying to call are actually methods of JDatabase, which you create an instance of at the end. 您尝试调用的方法实际上是JDatabase的方法,您将在最后创建一个实例。 So, you should be able to do $db->createTable(... etc and it will actually run. 因此,您应该能够执行$ db-> createTable(...等操作,它将实际运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM