我有两个实体payroll week和payroll period,想编写一个查询,检查用户输入到payroll period表的开始日期和结束日期是否在payroll week表中找到,payroll week表已经填充,并用数据库中的payroll period id更新它。每节课都是这样。这就是我到目前为止对查询所做的。
Payrollperiod实体:
<?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();
}
}
Payrollweek实体存储库:
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(),
);
}
最佳答案
首先需要获取PayrollPeriod id,然后相应地更新Payrollweek实体。您甚至不需要任何特殊的存储库功能。如果你在一个控制器里,它会是这样的:
$em = $this->getDoctrine()->getManager();
$payrollweek = // do whatever here to get a Payrollweek entity
$payrollperiod = $em->getRepository('agripayroll:\PayrollPeriod)
->findOneBy(['startDate' => $startDate, 'endDate' => $endDate]);
if ($payrollperiod) {
$payrollweek->setPayrollperiod($payrollperiod);
$em->persist($payrollweek);
$em->flush();
}
您甚至不需要使用任何自定义存储库函数,尽管将它们放在其中也不会造成伤害。我还将您的
$payrollperiodid
类成员更改为$payrollperiod
,因为它引用相应的Payrollperiod实体,而不是单个外键整数I d。关于php - Symfony/Doctrine-根据现有数据库值查询日期值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45089360/