symfony2子表单,一对多,只有在不存在时才会保留

时间:2013-01-01 15:58:26

标签: forms symfony doctrine one-to-many persist

我知道几天前我在这些问题上看到了一个问题的答案,但经过2个小时的搜索却没有运气,我再问一遍。遗憾。

我有一个表单,AlertType通过

调用子表单,StockType
->add('stock', new StockType(), array(
            "em" => $em,
        ));

我的StockType是

 $em = $options['em'];
    $transformer = new TickerToStockTransformer($em);

    $builder
        ->add(
                $builder->create('ticker', 'text')
                //->addModelTransformer($transformer)
                )
        ->add('exchange')
    ;

我的模型变换器看起来像:

class TickerToStockTransformer implements DataTransformerInterface
{
/**
 * @var ObjectManager
 */
private $om;

/**
 * @param ObjectManager $om
 */
public function __construct(ObjectManager $om)
{
    $this->om = $om;
}

/**
 * Transforms an object (stock) to an int (id).
 *
 * @param  Stock|null $stock
 * @return integer
 */
public function transform($stock)
{
    if (null === $stock) {
        return "";
    }

    return $stock->getId();
}

/**
 * Transforms an integer (id) to an object (stock).
 *
 * @param  integer $id
 * @return Stock|null
 * @throws TransformationFailedException if object (stock) is not found.
 */
public function reverseTransform($id)
{
    if (!$id) {
        return null;
    }

    $stock = $this->om
        ->getRepository('TSNStatsBundle:Stock')
        ->findOneById($id)
    ;

    if (null === $stock) {
        throw new TransformationFailedException(sprintf(
            'A stock with number "%s" does not exist!',
            $id
        ));
    }

    return $stock;
}

我的控制器很标准:

 $em = $this->getDoctrine()
            ->getEntityManager();
    $guru = $this->getRequest()->request->all();

    $guru =$guru['tsn_statsbundle_alerttype']['guru'];

    $guru = $em->getRepository("TSNStatsBundle:Guru")->find($guru);

    $time = new \DateTime(2012-12-30);
    $time->setTime(23,59);

    $alert  = new Alert();
    $alert->setText($time)
        ->setEmail($time)
        ->setTwitter($time)
        ->setChat($time);
    $request = $this->getRequest();
    $form = $this->createForm(new AlertType(), $alert, array(
            "em" =>$this->getDoctrine()->getEntityManager(),
            'guru' => $guru,
        ));
    $form->bindRequest($request);

    if ($form->isValid()) {
         $em = $this->getDoctrine()
                   ->getEntityManager();
        $em->persist($alert);
        $em->persist($alert->getStock());
        $em->flush();

        return $this->redirect($this->generateUrl('new_alert', array(
                'guru' => 2
            ) ));
    }

我目前没有使用变压器,因为它似乎没用。 (我知道它说的是FindOneById,它是在自动收报机(文本)上。我也尝试过byTicker,它也不起作用)

我与一个股票有多对一的关系,可以有很多警报。

表单效果很好,直到我试图保持已经存在“股票”的“警报”。如果股票不存在,我怎么才能坚持,否则只使用参考?

编辑: 好吧,所以我破解了我的isValid块,所以它可以工作,但它很难看。有没有更好的办法?

        if ($form->isValid()) {
         $em = $this->getDoctrine()
                   ->getEntityManager();
         if ($em->getRepository('TSNStatsBundle:Stock')->findOneByTicker($alert->getStock()->getTicker()))
             $alert->setStock($em->getRepository('TSNStatsBundle:Stock')->findOneByTicker($alert->getStock()->getTicker()));

        $em->persist($alert);
        $em->flush();

        return $this->redirect($this->generateUrl('new_alert', array(
                'guru' => 2
            ) ));
    }

0 个答案:

没有答案