I need an advice for my Symfony 3.4 project (yes I know there is Symfony 6), I have an entity Product and I created a method that calculates the completion percentage of a Product sheet. It checks many properties of the entity and some other entities related to it (collections).
For now, I placed that method inside my Product entity and it works well. But for a specific thing, I need to do a complex query to the database and I can't use the query builder in the Entity class. So I'm wondering if I should place that code in the ProductController or maybe in the ProductRepository ?
Is it possible to use the entity object in the repository ? I don't need to build queries for each check, I simply use entity getters for the most of the checks.
Then, I will show the result in several pages of my project.
My function is someting like this (simplified) :
public function checkSetup()
{
$setup = array(
'active' => $this->isActive(),
'ref' => !empty($this->ref) ? true : false,
'tags' => $this->tags->isEmpty() ? false : true,
);
// I want to add the following part :
$qb = $em->getRepository(Product::class)->createQueryBuilder('p');
// build complex query...
$records = $qb->getQuery()->getResult();
$setup['records'] = !empty($records) ? false : true;
// Completion level
$score = 0;
foreach ($setup as $s) {
if ($s) $score++;
}
$num = $score / count($setup) * 100;
$setup['completion'] = round($num);
return $setup;
}
/src/Checker/ProductChecker.phpand register it as a service ? Then inject EntityManager and do all the stuff. I'll just have to call the service in my Controller to render the result in a view. I'm gonna try that.