Sue
Sue

Reputation: 105

Custom Repository not working in Symfony

I have created a custom repository but it doesn't seems to be working. i get this error:

Entity 'com\twcl\agripayrollBundle\Entity\Payrollweek' has no field 'payrollweek'.

You can therefore not call findByPayrollweek() on the entity's repository. I know there's no payrollweek field but what would I change it to? When I tried the other fields I get an error also.

Payrollweek Repository

namespace com\twcl\agripayrollBundle\Entity;

use Doctrine\ORM\EntityRepository;

class PayrollweekRepository extends EntityRepository
{
    public function findByPayrollweek($startDate, $endDate)
    {
        return $this->_em
            ->createQuery(
                'SELECT pw FROM comtwclagripayrollBundle:Payrollweek pw
                WHERE pw.startDate = :startDate or pw.endDate = :endDate'
            )
            ->setParameter('startDate', $startDate)
            ->setParameter('endDate', $endDate)
            ->getResult();
    }
}

Controller - createAction

$payrollweek = $em->getRepository('comtwclagripayrollBundle:Payrollweek')
    ->findByPayrollweek([
        'startdate' => $form->get('startDate')->getData(),
        'enddate' => $form->get('endDate')->getData()
    ]);

    //If entity exists
    if ($payrollperiod) {
        $this->addFlash('error', 'Payroll Period exist.');

        return $this->redirect($this->generateUrl('payrollperiod'));
    }
    //If PayrollWeek matches the Payrollperiod entered
    elseif ($payrollweek) { 
        $payrollweek->setPayrollperiodid($entity);
        $em->persist($entity);
        $em->flush();

        $this->addFlash('error', 'Payroll Period was added.');
        $this->addFlash('error', 'Payroll week was updated.');
        return $this->redirect($this->generateUrl('payrollperiod'));
        //return $this->redirect($this->generateUrl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid())));
    }
    else {
        $this->addFlash('error', 'Payroll Period was does not match a payroll week.');
        return $this->redirect($this->generateUrl('payrollperiod'));  
    }
}

Payrollweek Entity

<?php

namespace com\twcl\agripayrollBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Payrollweek
 *
 * @ORM\Table(name="PayrollWeek", indexes={@ORM\Index(name="IDX_1B4F90669AD94696", columns={"payrollperiodid"})})
 * @ORM\Entity(repositoryClass="com\twcl\agripayrollBundle\Entity\PayrollweekRepository")
 */
class Payrollweek
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="startDate", type="datetime", nullable=false)
     */
    private $startdate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="endDate", type="datetime", nullable=false)
     */
    private $enddate;

    /**
     * @var integer
     *
     * @ORM\Column(name="normalHours", type="integer", nullable=true)
     */
    private $normalhours;

    /**
     * @var integer
     *
     * @ORM\Column(name="numOfDays", type="integer", nullable=true)
     */
    private $numofdays;

    /**
     * @var Payrollperiod
     * @ORM\ManyToOne(targetEntity="Payrollperiod", inversedBy="payrollweeks")
     * @ORM\JoinColumn(name="payrollperiodid", referencedColumnName="id")
     */
    private $payrollperiod;


    public function setPayrollperiod(Payrollperiod $payrollperiod) {
        $this->payrollperiod = $payrollperiod;
        return $this;
    }

    public function getPayrollperiod() {
        return $this->payrollperiod;
    }

    private $payrollperiodid;

    public function getId() {
        return $this->id;
    }

    public function getStartdate() {
        return $this->startdate;
    }

    public function getEnddate() {
        return $this->enddate;
    }

    public function getNormalhours() {
        return $this->normalhours;
    }

    public function getNumofdays() {
        return $this->numofdays;
    }

    public function getPayrollperiodid() {
        return $this->payrollperiodid;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setStartdate(\DateTime $startdate) {
        $this->startdate = $startdate;
    }

    public function setEnddate(\DateTime $enddate) {
        $this->enddate = $enddate;
    }

    public function setNormalhours($normalhours) {
        $this->normalhours = $normalhours;
    }

    public function setNumofdays($numofdays) {
        $this->numofdays = $numofdays;
    }

    public function setPayrollperiodid($payrollperiodid) {
        $this->payrollperiodid = $payrollperiodid;
    }
}

Upvotes: 1

Views: 1630

Answers (1)

Jason Roman
Jason Roman

Reputation: 8276

After speaking with Sue, this was due to mixing mapping annotation formats. This is a common issue that a lot of people miss. When people generate their Doctrine entities from the database and use annotations, they forget to remove the *.orm.xml mapping files in AppBundle/Resources/config/doctrine

The Symfony documentation states:

If you want to use annotations, you must remove the XML (or YAML) files after running this command. This is necessary as it is not possible to mix mapping configuration formats

For more information you can look at the Add Mapping Information section of the Symfony Doctrine documentation.

Upvotes: 2

Related Questions