我遇到错误,当我尝试创建一条记录时,它给了我这个错误,错误:在C:\ wamp64 \ www \ AgripayrollsystemV2 \ AgriPayrollSystem \ src \ com中调用字符串上的成员函数setPayrollperiodid() \ TWCL \ agripayrollBundle \控制器\ PayrollperiodController.php。这一行:** $ pWeek-> setPayrollperiodid($ entity); **
实体
<?php
namespace com\twcl\agripayrollBundle\Entity;
//use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Payrollperiod
*
*/
class Payrollperiod
{
/**
* @var integer
*
*/
private $payrollperiodid;
/**
* @var \DateTime
*
* @Assert\DateTime()
*/
private $startdate;
/**
* @var \DateTime
*
*
* @Assert\Type("DateTime")
* message="The end date must be after the start date")
*/
private $enddate;
/**
* @var integer
*
*
*/
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();
}
}
控制器
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());
//If entity exists
if ($payrollperiod){
$this->addFlash('error', 'Payroll Period exist.');
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('error', 'Payroll Period was added.');
$this->addFlash('error', '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(),
);
}
自定义存储库
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();
}
答案 0 :(得分:0)
问题是您从存储库返回计数而不是实体。
因此,如果您想设置外键,可以将存储库更改为:
return $this
->createQueryBuilder('pw')
->select('pw')
->from ('comtwclagripayrollBundle:Payrollweek','pw')
->where ('pw.startdate = :startdate and pw.enddate = :enddate')
->setParameter('startdate', $startdate)
->setParameter('enddate', $enddate)
->getQuery()
->getResult();
在控制器内部试试这个:
elseif (null != $payrollweek){
foreach ($payrollweek as $entity) {
$pWeek->setPayrollperiodid($entity);
$em->persist($pWeek);
}
}
如果您想计算行数,请使用其他函数来执行此操作
在您的存储库中,您可以使用以下内容:
class PayrollweekRepository
{
public function findBystartdateAndenddate($startdate, $enddate)
{
return $this
->createQueryBuilder('pw')
->select('pw')
->from ('comtwclagripayrollBundle:Payrollweek','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();
}
}
在你的控制器内你可以这样称呼:
$counter = $em->getRepository('comtwclagripayrollBundle:Payrollweek')->countBystartdateAndenddate($form->get('startdate')->getData(), $form->get('enddate')->getData());