Hi,
I've got a question regarding the Phalcon ORM. All my datatables have a field to make sure that they are all soft deleted. (is_deleted)
Now when I try to access a child of the found object I still get the childs that are deleted aswell. For instance:
$users = Users::find(); //Now only finds users with is_deleted = 0
$users->getTickets(); //Returns all tickets, also the ones who have is_deleted = 1
My search function that overrides all find actions (find, findFirst and count):
/**
* Function to search for all undeleted models
* @param type $parameters
* @return type
*/
public static function getSoftDeleteParams($parameters = null) {
//Set the deleted field name
$deletedField = "is_deleted";
if ($parameters === null) {
//if there are no params given, make the deleted check
$parameters = $deletedField . ' = 0';
} else if (is_numeric($parameters)) {
//the user is probably getting a single instance of a model by primary key (id)
$parameters = 'id = ' . $parameters . ' AND ' . $deletedField . ' = 0';
} else if (is_array($parameters) === false && strpos($parameters, $deletedField) === false) {
//If the parameter is a string (giving conditions to the query) concatenate is_deleted check to the params string
$parameters .= ' AND ' . $deletedField . ' = 0';
} else if (is_array($parameters) === true) {
//The parameters variable is an array of parameters, add is_deleted check to the first parameter
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) {
//Else concatenate it to the conditions string given in the array.
$parameters['conditions'] .= ' AND ' . $deletedField . ' = 0';
}
}
return $parameters;
}
Is there a way that i can make sure that i never get deleted childs back when accessing them using the magic getter? I hope that there is a way to filter out all deleted values while still using the ORM.
Thanks in advance!