What is the best practice according the Symfony way ?
Option 1 :
class MyController extends Controller
{
public function myAction(...)
{
// ..
$example = $this->getDoctrine()->getRepository('AppBundle:Contract')->getContracts(array(
'company_id' => $company->getId(),
'contract_month_date_start' => date('Y-m-d', strtotime('first day of this month', $contractDate->getTimestamp())),
'contract_month_date_end' => date('Y-m-d', strtotime('last day of this month', $contractDate->getTimestamp())),
));
// ..
}
}
class ExampleRepository extends EntityRepository
{
public function getContracts($options)
{
//..
$qb->select('contract')
->from('AppBundle:Contract', 'contract')
->where('contract.companyId = :company_id')
->andWhere('contract.startDate < :contract_month_date_end')
->andWhere('contract.endDate < :contract_month_date_end')
->setParameter('company_id', $options['company_id'])
->setParameter('contract_month_date_start', $options['contract_month_date_start'])
->setParameter('contract_month_date_end', $options['contract_month_date_end']);
//..
}
}
Option 2 :
class MyController extends Controller
{
public function myAction(...)
{
// ..
$example = $this->getDoctrine()->getRepository('AppBundle:Contract')->getContracts(array(
'company' => $company,
'contractDate' => $contractDate
));
// ..
}
}
class ExampleRepository extends EntityRepository
{
public function getContracts($options)
{
//..
$company_id = $options['company']->getId();
$contract_month_date_start = date('Y-m-d', strtotime('first day of this month', $options['contractDate']));
$contract_month_date_end = date('Y-m-d', strtotime('last day of this month', $options['contractDate']));
$qb->select('contract')
->from('AppBundle:Contract', 'contract')
->where('contract.companyId = :company_id')
->andWhere('contract.startDate < :contract_month_date_end')
->andWhere('contract.endDate < :contract_month_date_end')
->setParameter('company_id', $company_id)
->setParameter('contract_month_date_start', $contract_month_date_start)
->setParameter('contract_month_date_end', $contract_month_date_end);
//..
}
}
I feel like the second one is better (less duplicate code between the differents controllers using the repository). But I also feel like it gives too much responsibility to the Repository.