0

I'm usual working with symfony from the 2.1 or 2.2 versions. Today i started a new project on the 2.3 and i'm encountering problems to create my custom entity repository.

My entity is:

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

I dont write all variable and get and sets functions. I've created a repository file: Acme\MyBundle\Entity\AnnualproductionRepository.php

The code for the repository file is the following:

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

I call the repository in one of my controller, with the following code:

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

But i get this error like the repository doesnt exist and the controller get the function name like a default findBy* on one of the Entity attributes.

ERROR: *> Entity 'Acme\MyBundle\Entity\AnnualProduction' has no field

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

Have you some advise to solve this problem? the code seems to be identical to the one i usualy add to include custom entity repository in symfony 2.2 but for some reason it refuse to work. Tx for your time and Help.

groot
  • 57
  • 1
  • 9

2 Answers2

2

Problem Solved, the fact was that the entity.orm.xml files, stored in /src/acme/myBundle/config/doctrine, have an hight priority over the entity files, so every modification to the entity that i was doing wasent readed.

SOLUTION: Delete all the entity.orm.xml files after done the annotation ed the generation of the entity via terminal php command.

groot
  • 57
  • 1
  • 9
0

The namespace for your repository is wrong. You should have Acme\MyBundle\Entity\Repository for a namespace instead. The path to your file ought to then be Acme/MyBundle/Entity/Repository/AnnualProduction.

You should also let doctrine generate all your entities for you via

 php app/console doctrine:generate:entities Acme

You will then see a folder called Repositories in your Entities folder and thats where all the entities need to be stored.

Dr.Knowitall
  • 10,080
  • 23
  • 82
  • 133
  • tx for your reply. I've changed the namespaces in the entity annotation FROM: @ORM\Entity(repositoryClass="Acme\MyBundle\Entity\AnnualConsumptionRepository") TO: @ORM\Entity(repositoryClass="Acme\MyBundle\Entity\Repository\AnnualConsumption") Then i usend the command : php app/console doctrine:generate:entities Acme but it generated me only the entities, no Repository folder created and not not repository file created. Do i'm doing something wrong? – groot Oct 15 '13 at 08:51
  • I've found the answare to the fact that dont generate the entity/repository automaticaly in this post: http://stackoverflow.com/questions/14115143/symfony2s-doctrinegenerateentities-doesnt-generate-repo-classes i've in the facts to generate the folder and the files my myself like i was doing. In this case there are no difference between my initial configuration and your suggestion. I've tryed your namespace with the entity/repository forlder and the files generated manualy, but the problem rest the same. – groot Oct 15 '13 at 09:43