I'm using symfony 2.3. I'm trying to generate an Invoice for every commercial every end of a month. I implemented a Listener called InvoiceListener in order to that. The problem is updateInvoice() function doesn't insert the data into invoice table. The system doesn't render errors. The doctrine debuger in displaying the query correctly.
InvoiceListener.php
class InvoiceListener {
private $em;
public function __construct(\Doctrine\ORM\EntityManager $em) {
$this->em = $em;
}
public function process(GetResponseEvent $event) {
$expire = '2012-01-01';
$current_month = date("m");
$current_year = date("y");
if (date("d") == 1) {
echo 'first day';
$this->updateStatus($current_month, $current_year);
$this->updateInvoice();
} else {
echo 'not first day';
$users = $this->findByRole('ROLE_COMMERCIAL');
// update invoice
$this->updateInvoice($users);
}
}
public function updateStatus($current_month, $current_year) {
$queryBuilder = $this->em->createQueryBuilder();
$queryBuilder
->update('Biginfo\UserBundle\Entity\User', 'u')
->set('u.nbrBusiness', 1)
->set('u.month', $current_month)
->set('u.year', $current_year);
return $queryBuilder->getQuery()->getResult();
}
public function updateInvoice($users) {
$queryBuilder = $this->em->createQueryBuilder();
foreach ($users as $user) {
$queryBuilder
->update('Biginfo\AdminBundle\Entity\Invoice', 'c')
->set('c.commercial', $user->getId())
->set('c.month', $user->getMonth())
->set('c.year', $user->getYear())
->set('c.nbrBusiness', $user->getNbrBusiness());
}
return $queryBuilder->getQuery()->getResult();
}
/**
* @param string $role
*
* @return array
*/
public function findByRole($role) {
$qb = $this->em->createQueryBuilder();
$qb->select('u')
->from('Biginfo\UserBundle\Entity\User', 'u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%"' . $role . '"%');
return $qb->getQuery()->getResult();
}
}