我有两个实体工资周和工资单期间,想要编写一个查询来检查用户输入到工资周期表的开始日期和结束日期是否在已经填充的工资单周表中找到,并使用数据库中的工资单周期 ID 更新它。每个类看起来像。这就是我到目前为止对查询所做的。
工资期实体:
<?php
namespace comtwclagripayrollBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* Payrollperiod
*
* @ORMTable(name="PayrollPeriod")
* @ORMEntity
*/
class Payrollperiod
{
/**
* @var integer
*
* @ORMColumn(name="payrollperiodid", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $payrollperiodid;
/**
* @var DateTime
*
* @ORMColumn(name="startDate", type="datetime", nullable=false)
*/
private $startdate;
/**
* @var DateTime
*
* @ORMColumn(name="endDate", type="datetime", nullable=false)
*/
private $enddate;
/**
* @var integer
*
* @ORMColumn(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 comtwclagripayrollBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
/**
* Payrollweek
*
* @ORMTable(name="PayrollWeek", indexes={@ORMIndex(name="IDX_1B4F90669AD94696", columns={"payrollperiodid"})})
* @ORMEntity
*/
class Payrollweek
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var DateTime
*
* @ORMColumn(name="startDate", type="datetime", nullable=false)
* @AssertType("DateTime")
*/
private $startdate;
/**
* @var DateTime
*
* @ORMColumn(name="endDate", type="datetime", nullable=false)
* @AssertType("DateTime")
* @AssertExpression("this.getStartDate() < this.getEndDate()",
* message="The end date must be after the start date")
*/
private $enddate;
/**
* @var integer
*
* @ORMColumn(name="normalHours", type="integer", nullable=true)
*/
private $normalhours;
/**
* @var integer
*
* @ORMColumn(name="numOfDays", type="integer", nullable=true)
*/
private $numofdays;
/**
* @var Payrollperiod
*
* @ORMManyToOne(targetEntity="Payrollperiod")
* @ORMJoinColumns({
* @ORMJoinColumn(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 DoctrineCommonCollectionArrayCollection();
}
}
工资单周实体存储库:
namespace comtwclagripayrollBundleEntity;
use DoctrineORMEntityRepository;
/**
* 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(),
);
}
首先,您需要获取工资周期 ID,然后相应地更新您的工资单周实体。 您甚至不需要任何特殊的存储库功能。 如果您在控制器中,它将如下所示:
$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
,因为它引用了相应的工资周期实体,而不是单个外键整数 id。
下面是一个例子。如果您的工资单周已经存在,您需要支付,您可以构建表单以返回工资单周期以供选择,而不是在提交表单后查询它。
在此示例中,领土属于一个国家/地区。因此,该表单将显示国家/地区列表。
$builder
->add('name', TextType::class, [
'label' => 'Territory Name',
])
->add('abbrev', TextType::class, [
'label' => 'Territory Abbreviation',
])
->add('belongingCountry', EntityType::class, [
'class' => 'AppBundle:AddressCountry',
'choice_label' => 'name',
'label' => 'Country'
]);
在您的情况下,如果工资单周属于工资周期。你也可以这样做。
$builder
->add('payrollperiodid', EntityType::class, [
'class' => 'AppBundle:Payrollperiod',
'choice_label' => 'startdate'
])
您将不得不稍微弄乱choice_label才能根据需要格式化DateTime
的显示。