我通常使用2.1或2.2版本的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''Entity''YearualproductionRepository.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''Entity''YearualProduction"没有字段
"年月日"。因此,您不能在 实体存储库***
你有一些建议来解决这个问题吗? 该代码似乎与我通常添加的代码相同,以在Symfony 2.2中包含自定义实体存储库,但由于某种原因它拒绝工作。Tx 为您的时间和帮助。
问题解决了,事实是存储在/src/acme/myBundle/config/doctrine 中的 entity.orm.xml 文件比实体文件具有很高的优先级,因此对我正在做的实体所做的每次修改都会读取。
解决方案:通过终端 php 命令完成实体生成的注释后,删除所有 entity.orm.xml 文件。
存储库
的命名空间错误。您应该为命名空间使用 Acme''MyBundle''Entity''Repository。文件的路径应该是Acme/MyBundle/Entity/Repository/YearualProduction。
你还应该让教义为你生成所有的实体,通过
php app/console doctrine:generate:entities Acme
然后,您将在"实体"文件夹中看到一个名为"存储库"的文件夹,该文件夹需要存储所有实体。