我有两个实体工资核算周和工资核算期,想要编写一个查询,检查用户输入到工资核算期间表的开始日期和结束日期是否已在已填充的工资核算周表中找到并更新在数据库中使用该工资核算期限ID。每个类看起来像。这就是我迄今为止对查询所做的工作。
<?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(),
);
}
答案 0 :(得分:1)
首先,您需要获取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实体,而不是单个外键整数id。
答案 1 :(得分:1)
这是一个例子。如果您的工资核算期已经存在,这是PayrollWeek所需的,您可以构建表单以返回PayrollPeriods以从中选择,而不是在提交表单后查询它。
在此示例中,Territory属于Country。因此,表单将显示国家/地区列表。
$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。你也可以这样做。
$builder
->add('payrollperiodid', EntityType::class, [
'class' => 'AppBundle:Payrollperiod',
'choice_label' => 'startdate'
])
您将不得不使用choice_label来根据需要格式化\DateTime
的显示。