1

我有 2 个具有多对一关系的表。

class Parent
{
  /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     *
     * @ORM\OneToMany(cascade={"persist"},targetEntity="Child", mappedBy="parent")
     */

    private $child;

  // getters and setters
}

class child{

     /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var Parent
     *
     * @ORM\ManyToOne(cascade={"persist"},targetEntity="Parent")
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     * })
     *
     */
    private $parent;

   // getters and setters

}
  1. 现在如何处理删除父表的一行时产生的异常?

  2. 如何在 Sonata Admin Generator 表单中显示消息?

提前致谢

4

1 回答 1

3

1:现在如何处理父表的一行被删除时产生的异常?

Sol:以下步骤应该可以解决问题

  1. 覆盖由 Sonata 管理包提供的 CRUDController。确保覆盖的 CRUDController 与原始 CRUDController 位于同一位置。即.YourProject/AdminBundle/Controller/CRUDController.php

  2. 将 deleteAction 和 batchActionDelete 从原始控制器复制到您的 Overridden 控制器。IE

    公共函数 deleteAction($id) {

        $id     = $this->get('request')->get($this->admin->getIdParameter());
    
        $object = $this->admin->getObject($id);
    
       if (!$object) {
            throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
        }
    
        if (false === $this->admin->isGranted('DELETE', $object)) {
            throw new AccessDeniedException();
        }
    
        if ($this->getRequest()->getMethod() == 'DELETE') {
    
         try {
    
                $this->admin->delete($object);
    
                $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');
    
            } catch (\Exception $e) {
    
                $this->get('session')->setFlash('sonata_flash_error', sprintf('Cannot Delete because it has Dependeny'));
            }
             return new RedirectResponse($this->admin->generateUrl('list'));
           }
    
        return $this->render($this->admin->getTemplate('delete'), array(
            'object' => $object,
            'action' => 'delete'
        ));
    }
    
  3. try catch 块应该负责其余的工作。

2.如何在 Sonata Admin Generator 表单中显示消息?

sol:缓存块中的消息将显示在奏鸣曲管理捆绑形式中

IE。

try {

                    $this->admin->delete($object);

                    $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');

                } catch (\Exception $e) {

                    $this->get('session')->setFlash('sonata_flash_error', sprintf('Cannot Delete because it has Dependeny'));
                }

笔记 。确保导入所有必要的文件

于 2012-10-17T10:17:56.310 回答