Symfony/Doctrine-针对现有数据库值查询日期值 [英] Symfony / Doctrine - query date values against existing database values

查看:79
本文介绍了Symfony/Doctrine-针对现有数据库值查询日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体的工资周和工资期间,想要编写一个查询,检查是否已在填充的工资周表中找到用户输入到工资周期表中的开始日期和结束日期,并进行更新数据库中具有该工资核算期间ID的数据.每个类看起来像.到目前为止,这是我针对查询所做的事情.

I have two entities payroll week and payroll period, want to write a query that checks if the start date and end date entered by the user to the payroll period table is found in the payroll week table which is populated already and update it with that payroll period id in the database. Each classes looks like. This is what I have done thus far regarding the query.

<?php

namespace com\twcl\agripayrollBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Payrollperiod
 *
 * @ORM\Table(name="PayrollPeriod")
 * @ORM\Entity
 */
class Payrollperiod
{
    /**
     * @var integer
     *
     * @ORM\Column(name="payrollperiodid", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $payrollperiodid;

    /**
     * @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="State", type="integer", nullable=false)
     */
    private $state;

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

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

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

    public function getState() {
        return $this->state;
    }

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

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

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

    public function setState($state) {
        $this->state = $state;
    }

    /**
     * Render a payrollPeriodID as a string.
     *
     * @return string
     */
    public function __toString()
    {
        return (string) $this->getPayrollperiodid();
    }
}

发薪周实体:

<?php

namespace com\twcl\agripayrollBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Payrollweek
 *
 * @ORM\Table(name="PayrollWeek", indexes={@ORM\Index(name="IDX_1B4F90669AD94696", columns={"payrollperiodid"})})
 * @ORM\Entity
 */
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)
     * @Assert\Type("DateTime")
     */
    private $startdate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="endDate", type="datetime", nullable=false)
     * @Assert\Type("DateTime")
     * @Assert\Expression("this.getStartDate() < this.getEndDate()",
     *     message="The end date must be after the start date")
     */
    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")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="payrollperiodid", referencedColumnName="id", nullable=false)
     * })
     */
    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;
    }

    /** * Render StartDate and EndDate as a string.
     * * * @return string */

    public function __toString()
    {
        return (string) $this->getStartdate()->format('d-M-y').'/'.$this->getEnddate()->format('d-M-y');

    }

    public function __construct()
    {
        $this->PayrollweekType = new \Doctrine\Common\Collection\ArrayCollection();
    }
}

工资周实体存储库:

namespace com\twcl\agripayrollBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * PayrollweekRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PayrollweekRepository extends EntityRepository
{
    public function payrollPeriodWeek(){
       return $this->EntityManager()->createquery(
               'select startDate and endDate from AcmeDemoBundle:Payrollweek 
               update payrollperiodid' 
               )->getresults() ;
    }
}

更改:

public function createAction(Request $request)
{
    $entity = new Payrollperiod();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $payrollweek=new Payrollweek();
        $payrollperiod=$em->getRepository('comtwclagripayrollBundle:PayrollPeriod') ->findOneBy(['startDate' => $startDate, 'endDate' => $endDate]);
        if ($payrollperiod) {
        $payrollweek->setPayrollperiod($payrollperiod);
        }
        else{
        $this->addFlash('error','ERROR! Not a valid payroll week');
         return $this->redirect($this->generateUrl('payrollperiod'));
        }

        $em->persist($payrollweek);
        $em->flush();

        //return $this->redirect($this->generateUrl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid())));
    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

推荐答案

这里是一个示例.如果某个PayrollWeek所需的工资核算期已经存在,则可以构建表单以返回PayrollPeriods以供选择,而不是在提交表单后进行查询.

Here is an example. If your payroll period already exists that is needed for a PayrollWeek, you can build your form to return PayrollPeriods to select from instead of querying for it after the form is submitted.

在此示例中,领土属于一个国家.因此,该表格将显示国家/地区列表.

In this example, Territory belongs to a Country. So the form will display a list of countries.

$builder
    ->add('name', TextType::class, [
        'label' => 'Territory Name',
    ])
    ->add('abbrev', TextType::class, [
        'label' => 'Territory Abbreviation',
    ])
    ->add('belongingCountry', EntityType::class, [
        'class'         => 'AppBundle:Address\Country',
        'choice_label'  => 'name',
        'label'         => 'Country'
    ]);

在您的情况下,如果PayrollWeek属于PayrollPeriod.您可以执行相同操作.

In your case, if PayrollWeek belongs to PayrollPeriod. You can do the same.

$builder
    ->add('payrollperiodid', EntityType::class, [
        'class'         => 'AppBundle:Payrollperiod',
        'choice_label'  => 'startdate'
    ])

您将不得不对choice_label进行一些修改,以根据需要设置\DateTime的显示格式.

You will have to mess with the choice_label a bit to format the display of the \DateTime as you want.

这篇关于Symfony/Doctrine-针对现有数据库值查询日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆