0

私は通常、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;
        }
    }

すべての変数と get および set 関数を記述するわけではありません。リポジトリ ファイルを作成しました: 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();
    }
}

次のコードを使用して、コントローラーの 1 つでリポジトリを呼び出します。

<?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);
*
*
*
*
   }
}

しかし、リポジトリが存在しないようなこのエラーが発生し、コントローラーはエンティティ属性の1つでデフォルトのfindBy *のような関数名を取得します。

エラー: * > エンティティ 'Acme\MyBundle\Entity\AnnualProduction' にフィールドがありません

'年月日'。したがって、エンティティのリポジトリで「findByYearMonthDay」を呼び出すことはできません***

この問題を解決するためのアドバイスはありますか? コードは、symfony 2.2 にカスタム エンティティ リポジトリを含めるために通常追加するものと同じようですが、何らかの理由で動作を拒否します。あなたの時間とヘルプのためのTx。

4

2 に答える 2

2

問題は解決しました。実際には、/src/acme/myBundle/config/doctrine に保存されている entity.orm.xml ファイルがエンティティ ファイルよりも優先度が高いため、エンティティに対するすべての変更が読み取られませんでした。

解決策: 端末の php コマンドを使用してエンティティの生成に注釈を付けた後、すべての entity.orm.xml ファイルを削除します。

于 2013-10-16T07:19:48.827 に答える
0

リポジトリの名前空間が間違っています。代わりに、名前空間に Acme\MyBundle\Entity\Repository が必要です。ファイルへのパスは Acme/MyBundle/Entity/Repository/AnnualProduction になります。

また、doctrine にすべてのエンティティを生成させる必要があります。

 php app/console doctrine:generate:entities Acme

次に、Entities フォルダーに Repositories というフォルダーが表示され、そこにすべてのエンティティを保存する必要があります。

于 2013-10-15T06:27:11.373 に答える