自定义存储库不在Symfony中工作

时间:2017-07-26 15:37:11

标签: symfony doctrine

我创建了一个自定义存储库但它似乎没有工作。我收到这个错误:

  

实体'com \ twcl \ agripayrollBundle \ Entity \ Payrollweek'没有字段'payrollweek'。

因此,您无法在实体的存储库中调用findByPayrollweek()。我知道没有payrollweek字段,但我会将其更改为什么?当我尝试其他字段时,我也会收到错误。

Payrollweek存储库

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实体

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

1 个答案:

答案 0 :(得分:2)

与Sue交谈后,这是由于混合了映射注释格式。这是很多人都错过的常见问题。当来自数据库的人generate their Doctrine entities并使用注释时,他们忘记删除AppBundle/Resources/config/doctrine

中的 *。orm.xml 映射文件

Symfony文档说明:

  

如果要使用注释,则必须在运行此命令后删除XML(或YAML)文件。这是必要的,因为无法混合映射配置格式

有关详细信息,请查看Add Mapping InformationSymfony Doctrine documentation部分。