【问题标题】:Symfony-2.3 entity repository errorSymfony-2.3 实体存储库错误
【发布时间】:2013-10-14 16:47:00
【问题描述】:

我通常使用 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\AnnualProduction' 没有字段

'年月日'。因此,您不能在 实体的存储库***

您对解决这个问题有什么建议吗?该代码似乎与我通常在 symfony 2.2 中添加的包含自定义实体存储库的代码相同,但由于某种原因它拒绝工作。 发送您的时间和帮助。

【问题讨论】:

    标签: php doctrine-orm repository entity symfony-2.3


    【解决方案1】:

    问题已解决,事实是存储在 /src/acme/myBundle/config/doctrine 中的 entity.orm.xml 文件比实体文件具有更高的优先级,因此我对实体所做的每一次修改没读过。

    解决方案:通过终端 php 命令完成注释生成实体后,删除所有 entity.orm.xml 文件。

    【讨论】:

      【解决方案2】:

      您的存储库的命名空间错误。您应该使用 Acme\MyBundle\Entity\Repository 作为命名空间。你的文件路径应该是 Acme/MyBundle/Entity/Repository/AnnualProduction。

      您还应该让教义通过

      为您生成所有实体
       php app/console doctrine:generate:entities Acme
      

      然后,您将在 Entities 文件夹中看到一个名为 Repositories 的文件夹,这就是所有实体需要存储的位置。

      【讨论】:

      • tx 供您回复。我已将实体注释中的命名空间从:@ORM\Entity(repositoryClass="Acme\MyBundle\Entity\AnnualConsumptionRepository") 更改为:@ORM\Entity(repositoryClass="Acme\MyBundle\Entity\Repository\AnnualConsumption")然后我使用命令: php app/console dictionary:generate:entities Acme 但它只生成了实体,没有创建存储库文件夹,也没有创建存储库文件。我做错了什么吗?
      • 我在这篇文章中找到了不自动生成实体/存储库这一事实的答案:stackoverflow.com/questions/14115143/… 我已经在事实中生成了我自己喜欢的文件夹和文件正在做。在这种情况下,我的初始配置和您的建议没有区别。我已经使用实体/存储库 forlder 和手动生成的文件尝试了您的命名空间,但问题仍然存在。
      猜你喜欢
      • 2016-10-08
      • 2013-07-14
      • 2012-07-20
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多