Exclude object result in controller

This is my environment:

I have an entity:

Object |____ id (integer) |____ foo (string) |____ configurations (collection) 

The configurations property is a set of Configuration objects displayed using oneToMany . In any case, it looks like this:

 Configuration |____ id (integer) |____ bar (string) |____ name (string, nullable) 

I have a service to get one object :

  • This service is bundled, which manages the objects of many projects.

  • This service does not know which project is invoking it.

  • This service is a set of filters, event listeners, and sql queries. (something around 2K lines)

  • Consider what cannot / does not want to update its receiving process for what I want to do here.

I have the only action that I want to act on in my controller as follows:

 $objMgr = $this->get('objectManager'); //get my service I talked before $object = $objMgr->findOneById(); //get my object return array('object' => $object);// render template 

I have many themes that control the end user screen with each of them:

 theme1 |____ object_details.html.twig theme2 |____ object_details.html.twig themeX |____ object_details.html.twig 

These object_details.html.twig have this code for printing configurations.

 {% for config in object.configurations %} {# div config.blablabla etc. #} {% endfor %} 

My question is:

According to the context above, can I exclude all configurations with the name property not null?

Answer Requirements:

  • The answer does not allow editing twig files (I know how to do this ^^)

  • I want to be sure that the Configuration exception will not be detected by the doctrine and will delete this config my database on a random flash somewhere in the code.

  • The answer may be: "Dude, we can’t do this." In fact, I am very curious to find out if this is possible. At the moment, I have no idea.

  • The answer should take into account some best practices :)

Waiting for comments: why not edit twig files ?: I have a lot of topics, I want to know if this can be done once.

Why not exclude the results in your db query ?: Because I want these configurations on my other pages and projects using this getter.

+50 for a better try :)

+5
source share
3 answers

based on my comments above:

One option that will not reflect on anything other than the view will be a (custom) decorator that provides the same api as the source object.

Thus, the idea, or perhaps even other consumers, would not even know that they are dealing with a special case.

For instance:

 <?php namespace App\Something; use Doctrine\Common\Collections\Criteria; class ObjectDecorator /* implement ObjectInterface // if needed */ { public function __construct(Object $object) { $this->base = $object; } public function getConfigurations() { //Using Criteria to make it cleaner than a loop. $criteria = Criteria::create(); $criteria->where(Criteria::expr()->isNull('name')); return $this->base->getConfigurations()->matching($criteria); } /* Override all your decorated entity getters with public function getFoo() { return $this->base->getFoo(); } */ } 

and in your controller use:

 return array('object' => new ObjectDecorator($object)); 

You can expand it in several ways:

  • Creation / configuration of extraction in the decorator-factory service.
  • create an interface that both objects (concrete and decorated) follow so that they can be transferred.
+2
source

You want to filter by criteria:

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections

And an example:

 // Entity class class Department { public function getEmployeesByEmployeeTypeId($employeeTypeId) { $criteria = Criteria::create(); $criteria->where(Criteria::expr()->eq('employeeType', $employeeTypeId)); return $this->employees->matching($criteria); } } 

in your case, the criteria will be similar to

 $criteria->where("name is not null"); return $this->configurations->matching($criteria); 
+4
source

I really do not understand the whole problem, but I understand that you want to get some elements from the list in essence and do not want to remove from the object for any reason. So, create a new function directly in the entity, in this function you can filter the list and then return the new collection. This way you do not compromise the object.

 /** * Object * * @ORM\Table() * @ORM\Entity */ class Object { /** * @var Collection * * @ORM\OneToMany(targetEntity="Configuration", mappedBy="object") */ private $configurations //... public function getConfigurationsWithName() { $subResult = new ArrayCollection(); foreach ($this->configurations as $configuration) { if ($configuration->getName() != null) { $subResult.add($configuration); } } return $subResult; } //... } 

Then in the branch

 {% for filteredObject in object.configurationsWithName %} ... {% endfor %} 

So, the structure looks for any getConfigurationsWithName inside the object and finds a new function. Hope this is what you need.

+1
source

Source: https://habr.com/ru/post/1237356/


All Articles