context->table('emailaddresses'); $existing = $table->where('lower(address) = lower(?)', $address); foreach($existing as $row) return $database->createModel('Emailaddress', $row); $activerow = $table->insert(['address' => $address]); return $database->createModel('Emailaddress', $activerow); } public function getAddress() { return $this->activerow->address; } public function setAddress($address) { $this->activerow->update(['address' => $address]); } private $laststatus; public function getLaststatus() { if(!is_null($this->laststatus)) return $this->laststatus; $sent = $this->activerow->related('finaldestinations'); $good = 0; $bad = 0; foreach($sent as $row) { if(is_null($row->sent_when)) continue; if(is_null($row->last_bounce)) $good++; else $bad++; } if($bad) { if($good) $status = 'questionable'; else $status = 'bad'; } else { if($good) $status = 'good'; else $status = 'unknown'; } return $this->laststatus = $status; } public function attach_to_addresslist($addresslist) { $this->context->table('addresslistentries') ->insert([ 'emailaddress' => $this->activerow->emailaddress, 'addresslist' => $addresslist, ]); } public function detach_from_addresslist($addresslist) { $this->context->table('addresslistentries') ->where([ 'emailaddress' => $this->activerow->emailaddress, 'addresslist' => $addresslist, ]) ->delete(); } }