I also have a question about SoftDelete.
The code I have seems to add the value I've specified, but the find() function does not leave out the deleted records. Below I have pasted my model. I do not want to return any records that have a deleted not NULL. On the find, my Vendor model has a relationship defined and it is work as expected.
<?php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Establishment extends Model {
public $id;
public $vendor_id;
public $name;
public $address1;
public $address2;
public $city;
public $state_id;
public $zip;
public $country_id;
public $description;
public $lat;
public $lng;
public $created;
public $updated;
public $deleted;
public function initialize() {
$this->setSource('establishments');
$this->addBehavior(new SoftDelete([
'field' => 'deleted',
'value' => date('Y-m-d H:i:s')
]));
$this->hasOne('vendor_id','Vendor','id');
$this->hasMany('id','Promotion','establishment_id');
$this->hasOne('state_id','State','id');
$this->hasOne('country_id','Country','id');
}
public function validation() {
if ($this->validationHasFailed() == true) {
return false;
}
}
/*
public function beforeDelete() {
$this->deleted = date('Y-m-d H:i:s');
}
*/
public function beforeValidationOnCreate() {
$this->created = date('Y-m-d H:i:s');
$this->updated = date('Y-m-d H:i:s');
}
public function beforeValidationOnUpdate() {
$this->updated = date('Y-m-d H:i:s');
}
public function deleteEstablishment($request) {
$user = $this->getDi()->getShared('session')->get('auth');
$vendor = Vendor::findFirstByUserId($user->id);
//make sure logged in vendor account matches the value passed by the UI
if ($vendor->id == $request->vendor_id) {
//make sure the establishment's vendor ID matches the currently logged in vendor
if ($this->vendor_id == $vendor->id) {
// Todo: delete current or upcoming promotions
// Todo: refund any points associated w/ those promotions
return $this->delete();
} else {
$this->appendMessage(new Message("Establishment not found."));
return false;
}
} else {
$this->appendMessage(new Message("Vendor not found."));
return false;
}
}
public function listEstablishments() {
$user = $this->getDi()->getShared('session')->get('auth');
$vendor = Vendor::findFirstByUserId($user->id);
$establishments = $vendor->getEstablishment();
$arr = [];
foreach($establishments as $establishment) {
$establishment->state = $establishment->getState();
$establishment->country = $establishment->getCountry();
$arr[] = $establishment;
}
return $arr;
}
public function saveEstablishment($request,$vendor_id) {
//Vendor can't edit an establishment that isn't theirs.
if (!empty($this->id) && $this->vendor_id != $vendor_id) {
$this->appendMessage(new Message("Establishment not found."));
return false;
}
if (!empty($request->description) && strlen($request->description) > 72) {
$this->appendMessage(new Message("Description must be fewer than 72 characters."));
return false;
}
$this->vendor_id = $vendor_id;
$this->name = $request->name;
$this->address1 = $request->address1;
$this->address2 = empty($request->address2) ? "" : $request->address2;
$this->city = $request -> city;
$this->state_id = $request->state->id;
$this->zip = $request->zip;
$this->country_id = $request->country->id;
$this->description = $request->description;
$this->lat = $request->lat;
$this->lng = $request->lng;
return $this->save();
}
}