2014-03-13 11 views
0

我看到一個可以使用與JTable的負載的陣列,我試過如下:的Joomla JTable中裝載數組和編輯字段

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)是:

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

,但我有沒有運氣。我不斷收到以下錯誤:

0 - 缺少字段在數據庫:TableDocument   1.

Documentation on JTable

任何幫助非常感謝。

回答

2

好吧,其實你做錯了。您可以使用少量字段值加載JTable,但它只返回1個結果;

實施例使用:

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

上面的代碼將搜索ID表項= 100,USER_ID = 101不能裝載這樣的2個表條目。

我的建議是:

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; 
} 
+0

啊謝謝,我明白了,你的建議似乎工作好了很多,唯一的事情是,它不更新:date_removed –

+0

似乎很好地工作,如果我換$ return || $ file-> store()左右。 –

+0

是的,在我的回答中可能有錯(沒有在現場測試過),更新了我的答案; – di3sel