Jesse Boyer
I'm using this ModelBase class for auto-ignoring of deleted rows.
/**
* Base model class
*
* @link https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model.html
*/
abstract class ModelBase extends Model
{
/**
* @inheritdoc
*
* @access public
* @static
* @param array|string $parameters Query parameters
* @return Phalcon\Mvc\Model\ResultsetInterface
*/
public static function find($parameters = null)
{
$parameters = self::softDeleteFetch($parameters);
return parent::find($parameters);
}
/**
* @inheritdoc
*
* @access public
* @static
* @param array|string $parameters Query parameters
* @return Phalcon\Mvc\Model
*/
public static function findFirst($parameters = null)
{
$parameters = self::softDeleteFetch($parameters);
return parent::findFirst($parameters);
}
/**
* @inheritdoc
*
* @access public
* @static
* @param array|string $parameters Query parameters
* @return mixed
*/
public static function count($parameters = null)
{
$parameters = self::softDeleteFetch($parameters);
return parent::count($parameters);
}
/**
* @access protected
* @static
* @param array|string $parameters Query parameters
* @return mixed
*/
public static function softDeleteFetch($parameters = null)
{
if (method_exists(get_called_class(), 'getDeleted') === false) {
return $parameters;
}
$deletedField = call_user_func([get_called_class(), 'getDeleted']);
if ($parameters === null) {
$parameters = $deletedField . ' = 0';
} elseif (
is_array($parameters) === false &&
strpos($parameters, $deletedField) === false
) {
$parameters .= ' AND ' . $deletedField . ' = 0';
} elseif (is_array($parameters) === true) {
if (
isset($parameters[0]) === true &&
strpos($parameters[0], $deletedField) === false
) {
$parameters[0] .= ' AND ' . $deletedField . ' = 0';
} elseif (
isset($parameters['conditions']) === true &&
strpos($parameters['conditions'], $deletedField) === false
) {
$parameters['conditions'] .= ' AND ' . $deletedField . ' = 0';
}
}
return $parameters;
}
}
If you'll use it, your model must extend ModelBase class, e.g.
class Robot extends ModelBase
{
/* your code here */
}
and also you need to define getDeleted() method, e.g.
class Robot extends ModelBase
{
/* your code here */
public function getDeleted()
{
return 'robot_deleted';
}
/* your code here */
}
This base class give you ability to use soft-delete not on all models, e.g. I don't use it in some parts of website, so i just don't define getDeleted() method. And don't forgot to use SoftDelete behavior from Phalcon, because this base class doesn't override delete() method.
P.S.
You don't need to override __callStatic() method because it only calls findFirst(), find() and count() methods.