简体   繁体   中英

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.

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

$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

Can't seem to find any reference to create except for this: 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.

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

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

The methods that you are trying to call are actually methods of JDatabase, which you create an instance of at the end. So, you should be able to do $db->createTable(... etc and it will actually run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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