/** * Execute the task */ public function run($request) { $units = array('seconds', 'hours', 'minutes', 'days', 'weeks', 'months', 'years'); $unit = $request->requestVar('unit'); if (!$unit || !in_array($unit, $units)) { throw new RuntimeException("Please specify a 'unit' parameter. Possible values: " . implode(', ', $units)); } $count = $request->requestVar('count'); if (!$count) { throw new RuntimeException("Please specify a 'count' parameter for the number of {$unit} you want to to keep."); } $stamp = strtotime("-{$count} {$unit}"); $date = date('Y-m-d H:i:s'); $count = SentEmail::get()->filter(array('Created:LessThan' => $date))->count(); DB::query("DELETE FROM \"SentEmail\" WHERE Created < '{$date}'"); echo "Deleted {$count} records."; }
/** * Persists a {@link SentEmail} record to the database * @return SentEmail */ protected function persist() { $record = SentEmail::create(array('To' => $this->To(), 'From' => $this->From(), 'Subject' => $this->Subject(), 'Body' => $this->Body(), 'CC' => $this->CC(), 'BCC' => $this->BCC(), 'SerializedEmail' => serialize($this))); $record->write(); return $record; }