2015-07-10 43 views
0

我正在使用symfony 2.3。我試圖爲每個月底的每個商業廣告生成發票。我實現了一個名爲InvoiceListener的監聽程序。問題是updateInvoice()函數不會將數據插入到發票表中。系統不會呈現錯誤。正確顯示查詢的原則debuger。
enter image description here InvoiceListener.phpsymfony2:使用eventListener插入數據

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(); 
    } 

} 
+3

「_...我試圖爲每個廣告的每一個月末產生一張發票......_」 - 聽起來像你需要[控制檯命令](http://symfony.com /doc/current/cookbook/console/console_command.html),並用cronjob調用它。 – BentCoder

+1

我同意@BentCoder。爲此使用一個監聽器是一種矯枉過正的行爲。 – tftd

+0

也同意@BentCoder,應該使用控制檯 – MouradK

回答

1

除非我失去了一些東西,你的代碼將只更新相同的記錄一遍又一遍,我不認爲這是你想要的。要生成新的發票,您應該像這樣更改:

public function updateInvoice($users) { 
    $queryBuilder = $this->em->createQueryBuilder(); 
    $queryBuilder->insert('Biginfo\AdminBundle\Entity\Invoice', 'c'); 
    foreach ($users as $user) { 
     $queryBuilder->values(array(
      'c.commercial' => $user->getId(), 
      'c.month' => $user->getMonth(), 
      'c.year'=> $user->getYear(), 
      'c.nbrBusiness' => $user->getNbrBusiness() 
     )) 
    } 
    return $queryBuilder->getQuery()->getResult(); 
} 

P.S.你也應該使用佔位符,請參閱http://doctrine-dbal.readthedocs.org/en/latest/reference/query-builder.html#binding-parameters-to-placeholders

編輯:「插入」命令

「插入」命令has been added in Doctrine 2.5.0 BETA 3,所以它可能不會在Symfony的2.3可用。另一種方法是使用普通的SQL,例如

public function updateInvoice($users) { 
    $connection = $this->container->get('doctrine.dbal.default_connection');  
    $values = array(); 
    foreach ($users as $user) { 
     $values[] = "(
      '" . $user->getId() . "', 
      '" . $user->getMonth() . "', 
      '" . $user->getYear() . "', 
      '" . $user->getNbrBusiness() . "' 
      )"; 
    } 
    $sql = "INSERT INTO invoice (`commercial`, `month`, `year`, `nbrBusiness`) VALUES " . implode("," , $values); 
    $statement = $connection->prepare($sql); 
    $statement->execute(); 
} 
+0

調用未定義的方法Doctrine \ ORM \ QueryBuilder :: insert() –

+0

編輯我的答案來說明 –