1

我有一个页面,管理员可以在其中创建新的时间段。现在,当管理员想要添加一个新时期时,我想检查是否还没有另一个时期。

这是我当前的代码,但我想<=并且>=不像他们应该在教义中那样工作?

$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
  try {
    $from = str_replace('/', '-', $event->getData()['from']);
    $to = str_replace('/', '-', $event->getData()['to']);

    $dateFrom = new \DateTime($from, new \DateTimeZone('Europe/Amsterdam'));
    $dateFrom = $dateFrom->format('Y-d-m');

    $dateTo = new \DateTime($to, new \DateTimeZone('Europe/Amsterdam'));
    $dateTo = $dateTo->format('Y-d-m');

    $query = $this->getModelManager()->createQuery('PeriodeBundle:Periode', 'p');

    $query->select()
      ->where(':from = p.from')
      ->andWhere(':to = p.to')
      ->setParameter('from', $dateFrom)
      ->setParameter('to', $dateTo);

    $data = $query->execute();

    if (count($data) > 0) {
      $event->getForm()->addError(new FormError('This period overwrites another existing period ('
         . $data[0]->getFrom()->format('d/m/Y') . ' to '
         . $data[0]->getTo()->format('d/m/Y') . ')'));
    }
  } catch (\Exception $e) {
      $event->getForm()->addError(new FormError('Date not valid'));
  }


 });

在我的数据库中,我有几个像这样简化的时期:period_id, from, to

有人可以指出我正确的方向吗?当我创建一个已经存在的句点时,我的数据为 NULL,当我在遥远的将来添加一个句点时,它会给我所有行的结果。 我读过ltegte但我不太清楚如何将它集成到这段代码中。

此代码在我的 PeriodBundle 管理员中。

4

2 回答 2

2

我不知道 Sonata 如何处理表单,但我认为您应该使用验证约束而不是侦听器。这是一个常见的表单示例:

private $em;

public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver
        ->setDefaults(array(

            ...

            'constraints' => array(
                new Callback(
                    array('callback' => array($this, 'validateForm'))
                )
            )
        ));
}

public function validateForm(MyEntity $myEntity, ExecutionContextInterface $context)
{
    $valid = true;

    $from = str_replace('/', '-', $myEntity->etFrom());
    $to = str_replace('/', '-', $myEntity->getTo());

    $dateFrom = new \DateTime($from, new \DateTimeZone('Europe/Amsterdam'));
    $dateFrom = $dateFrom->format('Y-d-m');

    $dateTo = new \DateTime($to, new \DateTimeZone('Europe/Amsterdam'));
    $dateTo = $dateTo->format('Y-d-m');

    $queryBuilder = $em->createQueryBuilder();

    $query = $queryBuilder
        ->select(array('p'))
        ->from('PeriodeBundle:Periode', 'p')
        ->where($queryBuilder->expr()->gte('p.from', ':from'))
        ->andWhere($queryBuilder->expr()->lte('p.to', ':to'))
        ->setParameters(array(
            'from' => $dateFrom,
            'to' => $dateTo
        ))
        ->getQuery();

    $data = $query->getArrayResult();

    if (count($data) > 0) {
      $valid = false;
    }

    // ....

    if (!$valid) {
        $context
            ->buildViolation('Invalid....')
            ->atPath('MYFIELD')
            ->addViolation()
        ;
    }

}
于 2014-12-24T17:41:51.317 回答
0

尝试使用 findBy 方法:

$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
try {
    $from = str_replace('/', '-', $event->getData()['from']);
    $to = str_replace('/', '-', $event->getData()['to']);

    $dateFrom = new \DateTime($from, new \DateTimeZone('Europe/Amsterdam'));
    $dateFrom = $dateFrom->format('Y-d-m');

    $dateTo = new \DateTime($to, new \DateTimeZone('Europe/Amsterdam'));
    $dateTo = $dateTo->format('Y-d-m');
    $periods_list=$this->getModelManager()->getRepository(YourClassPath)->findBy(array('from'=>$dateFrom,'to' =>$dateTo));
if (sizeof($periods_list) > 0) {
  $event->getForm()->addError(new FormError('This period overwrites another existing period ('
     . $data[0]->getFrom()->format('d/m/Y') . ' to '
     . $data[0]->getTo()->format('d/m/Y') . ')'));
}
} catch (\Exception $e) {
  $event->getForm()->addError(new FormError('Date not valid'));
}


});
于 2014-12-24T17:32:55.540 回答