我想编辑具有特定州的时段中的活动。 0无效,1有效,2有效,3封闭。如果句点的状态为1,则允许用户编辑。否则他们会收到错误。在测试即使状态为@ 1时,用户仍然会收到错误。 periodid是活动表中的外键,而state是另一个具有句点id的表具有主键。我在
中有这个存储库
public function findBytransid($payrollperiodid)
{
$repository=$this->getEntityManager()->getRepository('comtwclagripayrollBundle:Activity');
$qb = $repository->createQueryBuilder('a');
$qb->select('a','pp');
$qb->Join('a.payrollperiodid','pp');
$qb->where('pp.state = :state');
$qb->setParameter('state', 1);
return $results = $qb->getQuery()->getResult();
}
控制器
$entity = $em->getRepository('comtwclagripayrollBundle:Activity')
->createQueryBuilder('a')
->innerJoin('a.sectionid', 's')
->innerJoin('s.farmid', 'f')
->where('a.transid=:id')
->setParameter('id', $id)
->getQuery()->getSingleResult();
$state = $em->getRepository('AcmeDemoBundle:Activity')->findBytransid($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Activity entity.');
} elseif ($state) {
$this->addFlash('error', 'ERROR! Cannot edit, this activity is active.');
return $this->redirect($this->generateUrl('activity'));
} else {
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
修改 例如,用户点击期间与状态1匹配的活动,然后将其带到编辑表单页面,否则可以编辑此活动的错误
答案 0 :(得分:0)
我建议:
findBytransid
函数放入存储库。也许另一个
控制器需要它。findBytransid
函数,该函数不绑定到一个活动,但会返回一个活动数组。该函数名为findBytransid
,但它从不使用sayed id。findBytransid()
绑定到活动存储库但是,您将其命名为返回状态撰写州和活动请求:
public function findActivityByTransId ($id) {
$qb = $this->createQueryBuilder('a'); // because the function is placed in the repository
$qb->innerJoin('a.sectionid', 's')
->innerJoin('s.farmid', 'f')
->join('a.payrollperiodid','pp')
->where('a.transid=:id')
->setParameter('id', $id)
->getQuery()->getSingleResult();
}
然后在控制器中:
// ....
$activity = $repository->findActivityByTransId($id);
if ($activity == null) {
throw $this->createNotFoundException('Unable to find Activity entity.');
} else {
if ($activity->getParyrollPeriodId() == null || $activity->getParyrollPeriodId()->getState() != 1) {
$this->addFlash('error', 'ERROR! Cannot edit, this activity is active.');
return $this->redirect($this->generateUrl('activity'));
} else {
....
}
注意:由于您只需要一个实体,您可以利用doctrine延迟加载功能和内置的获取方法
$activity = $repository->find($id); //if transid is the primary key otherwise use findBy(array(...))
希望这有帮助