Is it possible to define a custom SQL query inside an object in Symfony3

looking for good practices here =)

Basically, I have one Entity that is related to Elems with the ManyToOne relation.

Suppose I want to select only some elements from my entity.

I can do

class Entity { 
    /* some vars here */ 
    public function getSpecificElems(){
        forEach($this->elems as $elem){
            /* do stuff here */
            if($someCondition){ $result[]=$elem;}
        }
        return $result;
    }

But it can mean large data samples when there are many elements associated with my Entity. Another way:

$em->getRepository("AppBundle:Repository")->getSpecificElems($entity);

Where getSpecificElemsdoes the DQL query.

I have a problem: the first solution is more intuitive for me, because it is OOP. The second is faster to complete, but it seems bad to me.

Is there a way to mix both of 1) and 2) to get $entity->getSpecificElems(), in order to return the list, I want to execute a good SQL query?

Greetings

+4

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


All Articles