Does Propel know when an object has been soft-deleted, so that child entities could still show their deleted parent?
I am soft-deleting objects in a MySQL database and using the Propel ORM. I have gotten soft-deleting to work, but at the cost of losing my enforced parent-child relationships, since the actual rows are not being deleted.
Is there any way for Propel to know that a record has been soft-deleted when you access it, so that a null-reference exception is not thrown? This way, although a parent has been deleted, its child can still read it's relation, but when updating a child, or creating a new child, the deleted parent is not accessible.
For example,
Book has an AuthorId, and if the author belonging to Au开发者_运维知识库thorId is soft-deleted, then:
$book->getAuthor();
would return the correct author (for viewing purposes only). However, if a new book was added, the author that was soft-deleted is not available to be selected.
Does anybody know if that functionality is built into Propel?
Soft delete is a broken abstraction. You should use the archivable behavior instead (see why in the Propel blog: http://propelorm.org/blog/2011/08/29/introducing-archivable-behavior-and-why-soft-delete-is-deprecated.html)
I'm not sure why an Author would be allowed to be deleted but his works would not (or, basically, why this comes up as a scenario in your project) but you can create custom criteria and execute it. The following code depends on the version of Propel you are using (but the concept remains the same):
$c = new Criteria();
$c->getNewCriterion(self::AUTHOR_ID, $parentId);
return self::doSelect($c, $connection);
Just stumbled across this question. Seems like it would make a lot more sense to not use soft delete for the functionality you are describing. I would highly advise that you create a field to flag whether authors are enabled, i.e. a boolean field called isEnabled.
You could then use the generated filter method for AuthorQuery class, in this case
AuthorQuery::create->filterByisEnabled()
->find();
If an object is still going to be used in the application it isn't really appropriate to delete. Soft delete functionality is only really for reference or revert mistakes.
精彩评论