I see that one can use an array with Jtable load, I tried the following:

public function delete($id = null) {
    $app = JFactory::getApplication();
    $id = $id ? $id : $app->input->get('files', array(), 'ARRAY');

    $file = JTable::getInstance('Document','Table');

    $file->load($id);
    $file->date_removed = date("Y-m-d H:i:s",time());

    if($file->store())
    {
        return true;
    } else {
        return false;
    }
}

print_r($id) is:

Array
(
    [0] => 1
    [1] => 2
)

But I am having no luck. I keep getting the following error:

0 - Missing field in database: TableDocument   1.

Documentation on JTable

Any Help Greatly Appreciated.

有帮助吗?

解决方案

Well actually you doing it wrong. You can load JTable with few field values, but it only returns 1 result;

Example use:

$data = array('id' => 100, 'user_id' => 101);
$table->load($data);

The code above will search for table entry with id = 100 AND user_id = 101. You cannot load 2 table entries like that.

My suggestion would be:

public function delete($id = null) {
  $ids = array();
  $return = true;

  if ($id) {
    $ids[] = $id;
  } else {
    $ids = JFactory::getApplication()->input->get('files', array(), 'ARRAY');
  }

  if (count($ids) > 0) {
    foreach ($ids as $id) {
      $file = JTable::getInstance('Document','Table');
      $file->load($id);
      $file->date_removed = date("Y-m-d H:i:s",time());
      $temp = $file->store();
      $return = $return || $temp;
    }
  }
  return $return;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top