On hook_user_login我想要刪除超過21天的給定內容類型的登錄用戶擁有的節點。下面drupal 7 | db_select | date_field條件
的代碼工作正常(無日期條件):
function mymodule_user_login(&$edit, $account) {
global $user;
$myuid = $user->uid;
$mytype = 'testdelete';
$mydate = '2016-07-06 18:45:00';
$results = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', $mytype)
->condition('uid', $myuid)
->execute();
foreach ($results as $result) {
$nids[] = $result->nid;
}
if (!empty($nids)) {
node_delete_multiple($nids);
drupal_set_message(t('%count nodes has been deleted', array('%count' => count($nids))));
}
}
現在我因爲堅持了一個多星期與日期條件。我的日期字段是內容類型「testdelete」被稱爲「datum」,窗口小部件是彈出日曆。
我已經試過:
->fields('n', array('nid','field_datum'))
和
->fields('n', array('nid','datum'))
兩個結果異常:
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'n.field_datum' in 'field list': SELECT n.nid AS nid, n.field_datum AS field_datum FROM {node} n WHERE (type = :db_condition_placeholder_0) AND (uid = :db_condition_placeholder_1) ; Array ([:db_condition_placeholder_0] => testdelete [:db_condition_placeholder_1] => 4) in mymodule_user_login()
應該如何的情況看:
->condition('datum', $mydate,'<')
而$指明MyDate應該是這樣的:now() -21 days
另一個問題:
我擔心hook_user_login是不正確的地方這個工作還是可以嗎?
我應該使用像hook_cron一樣的東西嗎?
爲什麼不張貼在[Drupal Answers](http://drupal.stackexchange.com/)? –
對不起 - 我下次會這樣做 – user2699953