尝试执行自定义存储库时出现错误:
执行"选择 COUNT(p0_.id( 作为 sclr0 从工资单周 p1_ 与 (NOLOCK(,工资单周p0_与 (nolock( 时发生异常,其中 p0_.startDate = ?AND p0_.endDate = ?' 带参数 ["startDate", "endDate"]:
SQLSTATE[22007]:[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]从字符串转换日期和/或时间时转换失败。我有什么
public function createAction(Request $request)
{
$entity = new Payrollperiod();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$payrollperiod = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findOneBy(['startdate'=>$form->get('startDate')->getData(), 'enddate'=>$form->get('endDate')->getData()]);
$payrollweek = $em->getRepository('comtwclagripayrollBundle:Payrollweek')->findBystartdateAndenddate($form->get('startDate')->getData(), $form->get('endDate')->getData());
$counter = $em->getRepository('comtwclagripayrollBundle:Payrollweek')->countBystartdateAndenddate('startDate', 'endDate');
if ($counter){
//If entity exists
if ($payrollperiod){
$this->addFlash('error', 'Error: A payroll period is already present with the same start and end date.');
return $this->redirect($this->generateUrl('payrollperiod'));
}
//If PayrollWeek matches the Payrollperiod entered
elseif ($payrollweek){
foreach ($payrollweek as $pWeek) {
$pWeek->setPayrollperiodid($entity);
$em->persist($pWeek);
}
$em->persist($entity);
$em->flush();
$this->addFlash('success', 'Payroll Period was added.');
$this->addFlash('success', '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 does not match a payroll week.');
return $this->redirect($this->generateUrl('payrollperiod'));
}
}
//return $this->render('comtwclagripayrollBundle::new.html.twig',array('form' => $form->createView()));
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
}
public function findBystartdateAndenddate($startdate, $enddate)
{
return $this
->createQueryBuilder('pw')
->select('pw')
->where ('pw.startdate = :startdate and pw.enddate = :enddate')
->setParameter('startdate', $startdate)
->setParameter('enddate', $enddate)
->getQuery()
->getResult();
}
public function countBystartdateAndenddate($startdate, $enddate)
{
return $this
->createQueryBuilder('payrollweek')
->select('COUNT(pw)')
->from ('comtwclagripayrollBundle:Payrollweek','pw')
->where ('pw.startdate = :startdate and pw.enddate = :enddate')
->setParameter('startdate', $startdate)
->setParameter('enddate', $enddate)
->getQuery()
->getOneorNullResult();
}
我正在尝试计算工资周以匹配输入的工资单期。工资周期为两周,而工资周为一周。例如,用户输入 2017 年 8 月 6 日至 8 月 19 日,然后在工资表中输入两条记录 8 月 6 日至 12 日和 8 月 13 日至 19 日。计数会为此工作吗?
与其使用$form->get('startDate')
和$form->get('endDate')
尝试使用$entity->getStartDate()
和$entity->getEndDate()
,因为您传递的是字符串而不是Datetime
对象