2013-10-14 52 views
0

我通常使用2.1或2.2版本的symfony。 今天我在2.3上開始了一個新項目,並遇到了創建我的自定義實體存儲庫的問題。Symfony-2.3實體存儲庫錯誤

我的實體是:

<?php 

    namespace Acme\MyBundle\Entity; 

    use Doctrine\ORM\Mapping as ORM; 

    /** 
    * AnnualProduction 
    * 
    * @ORM\Table(name="Annual_Production") 
    * @ORM\Entity(repositoryClass="Acme\MyBundle\Entity\AnnualproductionRepository") 
    */ 
    class AnnualProduction 
    { 
     /** 
     * @var string 
     * 
     * @ORM\Column(name="device_address", type="string", length=45, nullable=true) 
     */ 
     private $deviceAddress; 

     /** 
     * @var integer 
     * 
     * @ORM\Column(name="mese_1", type="integer", nullable=true) 
     */ 
     private $mese1; 

     /** 
     * @var integer 
     * 
     * @ORM\Column(name="mese_2", type="integer", nullable=true) 
     */ 

SOME MISSING VAR SET AND GET 


     /** 
     * @var string 
     * 
     * @ORM\Column(name="sens_id", type="string", length=45) 
     * @ORM\Id 
     * @ORM\GeneratedValue(strategy="NONE") 
     */ 
     private $sensId; 

     /** 
     * @var \DateTime 
     * 
     * @ORM\Column(name="AAAA", type="date") 
     * @ORM\Id 
     * @ORM\GeneratedValue(strategy="NONE") 
     */ 
     private $aaaa; 

    /** 
     * Set deviceAddress 
     * 
     * @param string $deviceAddress 
     * @return AnnualProduction 
     */ 
     public function setDeviceAddress($deviceAddress) 
     { 
      $this->deviceAddress = $deviceAddress; 

      return $this; 
     } 

     /** 
     * Get deviceAddress 
     * 
     * @return string 
     */ 
     public function getDeviceAddress() 
     { 
      return $this->deviceAddress; 
     } 

     /** 
     * Set mese1 
     * 
     * @param integer $mese1 
     * @return AnnualProduction 
     */ 
     public function setMese1($mese1) 
     { 
      $this->mese1 = $mese1; 

      return $this; 
     } 

     /** 
     * Get mese1 
     * 
     * @return integer 
     */ 
     public function getMese1() 
     { 
      return $this->mese1; 
     } 
     /** 
     * Set sensId 
     * 
     * @param string $sensId 
     * @return AnnualProduction 
     */ 
     public function setSensId($sensId) 
     { 
      $this->sensId = $sensId; 

      return $this; 
     } 

     /** 
     * Get sensId 
     * 
     * @return string 
     */ 
     public function getSensId() 
     { 
      return $this->sensId; 
     } 

     /** 
     * Set aaaa 
     * 
     * @param \DateTime $aaaa 
     * @return AnnualProduction 
     */ 
     public function setAaaa($aaaa) 
     { 
      $this->aaaa = $aaaa; 

      return $this; 
     } 

     /** 
     * Get aaaa 
     * 
     * @return \DateTime 
     */ 
     public function getAaaa() 
     { 
      return $this->aaaa; 
     } 
    } 

我不寫所有的變量,並獲得和設置功能。 我創建了一個庫文件:ACME \ MyBundle \實體\ AnnualproductionRepository.php

存儲庫文件的代碼如下:

<?php 


namespace Acme\MyBundle\Entity; 


use Doctrine\ORM\EntityRepository; 
use Doctrine\ORM\NoResultException; 
use Acme\MyBundle\Entity\AnnualProduction; 

class AnnualproductionRepository extends EntityRepository 
{ 

    public function findByYearMonthDay($anno, $mese, $giorno, $sensId) 
    { 
     $query = $this->getEntityManager() 
     ->createQuery(" SOME QUERY HERE")->setParameters(array(SOME PARAMETERS                 HERE)); 
     return $query->getSingleResult(); 
    } 
} 

我所說的倉庫在我的控制器之一,下面的代碼:

<?php 

namespace Acme\MyBundle\Controller; 


use Acme\MyBundle\Entity\AnnualProduction; 
use Acme\MyBundle\Entity; 

use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Validator\Constraints\Date; 



class DataController extends Controller{ 

    public function indexUserAction(){ 

* 
* 
* 
* 
* 
    $DailyProduction=$DailyProduction+$em->getRepository('AcmeMyBundle:AnnualProduction')->findByYearMonthDay($year, $month, $day, $productionSensor); 
* 
* 
* 
* 
    } 
} 

,但我得到這個錯誤,如犯規存在倉庫和控制器獲取函數名稱,如在實體的一個屬性默認findBy *。

錯誤: *>實體 '的Acme \ MyBundle \實體\ AnnualProduction' 沒有場

'yearMonthDay'. You can therefore not call 'findByYearMonthDay' on the entities' repository***

你有沒有一些建議來解決這個問題?該代碼似乎與我通常添加到symfony 2.2中的自定義實體存儲庫相同,但由於某種原因它拒絕工作。 Tx爲您的時間和幫助。

回答

2

問題解決了,事實是存儲在/ src/acme/myBundle/config/doctrine中的entity.orm.xml文件比實體文件具有更高的優先級,所以每次修改實體做得很好。

解決方案:在通過終端php命令完成註解編輯實體的生成後,刪除所有entity.orm.xml文件。

0

您的存儲庫的命名空間是錯誤的。您應該爲命名空間提供Acme \ MyBundle \ Entity \ Repository。文件的路徑應該是Acme/MyBundle/Entity/Repository/AnnualProduction。

你也應該讓學說爲您生成的所有實體通過

php app/console doctrine:generate:entities Acme 

然後,您會看到一個文件夾,名爲庫在你的實體文件夾,存儲,所有的實體需要多數民衆贊成。

+0

tx爲您的答覆。我已經將實體註釋FROM:@ORM \ Entity(repositoryClass =「Acme \ MyBundle \ Entity \ AnnualConsumptionRepository」)中的名稱空間更改爲:@ORM \ Entity(repositoryClass =「Acme \ MyBundle \ Entity \ Repository \ AnnualConsumption」)然後我使用命令:php app/console doctrine:generate:entities Acme但它只生成實體,沒有創建Repository文件夾,也沒有創建存儲庫文件。我做錯了什麼? – groot

+0

我發現answare的事實,不生成在這篇文章中的實體/資料庫automaticaly:http://stackoverflow.com/questions/14115143/symfony2s-doctrinegenerateentities-doesnt-generate-repo-classes我在事實來生成我自己的文件夾和文件,就像我正在做的那樣。在這種情況下,我的初始配置和您的建議沒有區別。我已經嘗試過使用實體/資源庫forlder和生成文件的命名空間,但問題依然存在。 – groot