我通常使用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\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\ Entity’没有字段
“一年的一个月”因此,您不能在实体的存储库上调用'findByYearMonthDay‘*
你对解决这个问题有什么建议吗?代码似乎与我通常添加的代码相同,其中包括symfony 2.2中的自定义实体存储库,但出于某种原因,它拒绝工作。为了你的时间和帮助。
发布于 2013-10-16 07:19:48
问题解决了,事实是,存储在/src/acme/myBundle/config/ doing中的entity.orm.xml文件比实体文件具有很高的优先级,所以我对实体所做的每一次修改都是经过修改的。
解决方案:完成注释后删除所有entity.orm.xml文件,通过终端php命令生成实体。
发布于 2013-10-15 06:27:11
存储库的命名空间是错误的。您应该为名称空间使用Acme\MyBundle\Entity\Repository。然后,文件的路径应该是Acme/MyBundle/Entity/Repository/AnnualProduction.。
你也应该让教义为你产生所有的实体
php app/console doctrine:generate:entities Acme然后,您将在实体文件夹中看到一个名为存储库的文件夹,所有实体都需要在那里存储。
https://stackoverflow.com/questions/19365105
复制相似问题