Carlos Gonzalez
Carlos Gonzalez

Reputation: 151

Doctrine findBy with parameters self fetching

Very often I have to do this findBy(array('criteria'=> $someObject)where $someObject is a prepopulated object. My question is: is there a built-in function that given the $someObject id and his entity type fetch him from database and then executes the findBy from above??? Something like this:

public function findByDiscriminator($slave, $idSlave, $discriminator, $discriminatorCriteria)
{
    $slaveManager = $this->container->get('doctrine')->getRepository("AppBundle:$slave");
    $slaveEntity = $slaveManager->find($idSlave);
    return $this->findAllByCriteria(array($discriminatorCriteria => $slaveEntity), 0, 0, $discriminator);
}

public function findAllByCriteria($criteria, $limit = 0, $offset = 0, $repository)
{
    $total = $limit * $offset;
    $start = $limit * ($offset - 1);
    $spamManager = $this->container->get('doctrine')->getRepository("AppBundle:$repository");
    $spam = $spamManager->findBy($criteria, array(), $total != 0 ? $total : null, $start != 0 ? $start : null);
    return $spam;
}

If, lets say, I need to fetch al groceries whose supplier id is 456 I'd query like this DAO->findByDiscriminator('SupplierEntity', 456, 'GroceryEntity', 'supplier') assuming there is a field in GroceryEntity named supplier.

Basically is a findBy() who fetches the criteria from database before querying

Upvotes: 0

Views: 1102

Answers (1)

Valentin Knyazev
Valentin Knyazev

Reputation: 166

As far as I know there are no such built-in wrappers for findBy method provided by doctrine. You can define them in some base class and then inherit it by your doctrine data access objects which will finally bring reusability to your code that you are looking for.

Upvotes: 1

Related Questions