1

I would like to delete some items with state "trashed" via CLI. Is it possible to call the delete-method of JTable from CLI?

Thanks for your help :-)

4

It's been awhile since I've done this, but as I recall putting this within doExecute should do what you want.

$basePath = JPATH_ADMINISTRATOR.'/components/com_content';
require_once $basePath.'/models/article.php';
$artmodel = new ContentModelArticle(array('table_path' => $basePath.'/tables'));

$db = JFactory::getDbo();
$q = "SELECT * FROM #__content WHERE `state` = '-2'";
$db->setQuery($q);
if( $rows = $db->loadObjectList() ) {
    foreach($rows as $row){
        $this->out('Removing '.$row->title);
        $result = $artmodel->delete($row->id);
    }
}

In your case, you're deleting items that are already trashed (state=-2), but for the record, this code won't work unless that's the case. To delete a record that is NOT trashed, you have to trash it first, using the ->save method prior to the ->delete method:

$data = array('id' => $row->id,'state'=>'-2');
$result = $artmodel->save($data);
$result = $artmodel->delete($row->id);
  • I believe your forgot to define $db ;) – Lodder Jul 20 '15 at 14:20
  • 1
    fussy, fussy, fussy.....lol – GDP Jul 20 '15 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.