I am programming my first symfony project and I stuck at a problem: I have a entity I called it "load" which refers to other entities which a call "transaction" (x transactions belongs to 1 load) :
/**
* @ORM\OneToMany(targetEntity="Transaction", mappedBy="load")
*
*/
private $transactions;
The transactions can be flagged as deleted but they keep stored in the database. I created a custom repository and some methods which deliver me the undeleted transactions.
But if I want to fetch a load with all it's transactions, I call
$load = $loadRepository->find($id);
it does what it is supposed. It fetches all transactions from the database which are referenced to the given load.
But I don't want to have the deleted transactions in my result. How can I achive this? I have absolutely no approach. Sure, I can iterate over the transactions and remove the deleted ones but I think there is a better solution.
I tried to register a repository for the transactions (this works) and override the find-method (this doesn't work). Is there another method which is internally called and I have to override?
thank you in advance!
I found a solution:
#config.yml:
doctrine:
orm:
filters:
deletedFilter:
class: AppBundle\Filter\SoftDeletedFilter
enabled: true
_
namespace AppBundle\Filter;
use Doctrine\ORM\Mapping\ClassMetaData,
Doctrine\ORM\Query\Filter\SQLFilter;
class SoftDeletedFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
if(!is_subclass_of($targetEntity->rootEntityName, "AppBundle\Entity\SoftDeleteableInterface")){
return "";
}
return $targetTableAlias.'.deleted = 0';
}
}
Now, all entities which implements the (empty) interface "SoftDeleteableInterface" will not be included if the deleted-value is not 0.
Thank you anyway for the answers/comments
If I have correctly understand your question, this behavior already exist with the doctrine extension SoftDeletable, you can find documentation here
You just have to add something like deleteAt
property on your Transaction
entity then all entities with data inside this field will be automatically filtered like if they are really deleted. If you want to find all of the Transaction entities you can always disable the filter on a query.