更新实体多对多关系SonataAdmin

时间:2015-05-19 21:46:20

标签: symfony sonata-admin

我有5个实体

实体\图书:

class Book
{
/**
 * @var integer
 *
 * @ORM\Column(name="b_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $bId;

/**
 * @var string
 *
 * @ORM\Column(name="b_foto", type="string", length=255, nullable=true)
 */
private $bFoto;

/**
 * @var string
 *
 * @ORM\Column(name="b_title", type="string", length=255, nullable=false)
 */
private $bTitle;

public $author;

public $heading;
}

实体\作者

/**
* Author
*
* @ORM\Table(name="author")
* @ORM\Entity
*/
class Author
{
/**
 * @var integer
 *
 * @ORM\Column(name="a_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $aId;

/**
 * @var string
 *
 * @ORM\Column(name="a_firstname", type="string", length=100, nullable=false)
 */
private $aFirstname;

/**
 * @var string
 *
 * @ORM\Column(name="a_lastname", type="string", length=100, nullable=false)
 */
private $aLastname;
}

实体\标题

/**
* Heading
*
* @ORM\Table(name="heading")
* @ORM\Entity
*/
class Heading
{
/**
 * @var integer
 *
 * @ORM\Column(name="h_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $hId;

/**
 * @var string
 *
 * @ORM\Column(name="h_title", type="string", length=100, nullable=false)
 */
private $hTitle;
}

实体\ BooksAuthor

/**
* BooksAuthor
*
* @ORM\Table(name="books_author", indexes={@ORM\Index(name="book_in", columns={"b_id"}), @ORM\Index(name="author_in", columns={"a_id"})})
* @ORM\Entity
*/
class BooksAuthor
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \AppBundle\Entity\Book
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Book")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="b_id", referencedColumnName="b_id")
 * })
 */
private $b;

/**
 * @var \AppBundle\Entity\Author
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="a_id", referencedColumnName="a_id")
 * })
 */
private $a;
}

实体\ BooksHeading

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

/**
 * @var \AppBundle\Entity\Heading
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Heading")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="h_id", referencedColumnName="h_id")
 * })
 */
private $h;

/**
 * @var \AppBundle\Entity\Book
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Book")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="b_id", referencedColumnName="b_id")
 * })
 */
private $b;
}

我使用SonataAdmin,当我更新我的Entity \ Book时,我想要更新和另一个实体。

那是我的BookAdmin

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class BookAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('bTitle', null , array('label' => 'Название'))
        ->add('author', 'sonata_type_model', array('class' => 'AppBundle\Entity\Author',
                                        'label'=>"Авторы",
                                        'multiple'=>true,
                                        'required'=>false,
                                        'multiple'=>true,
                                        'expanded'=> true,
                                        'by_reference' => false,),
                                        array(
                                        'edit' => 'inline',
                                        'inline' => 'table',
                                        'sortable'  => 'position')    
                                        )
        ->add('heading', 'sonata_type_model', array('class' => 'AppBundle\Entity\Heading',
                                        'label'=>'Рубрика',
                                        'required'=>false,
                                        'multiple'=>true,
                                        'expanded'=> true,
                                        'by_reference' => false,),
                                        array(
                                        'edit' => 'inline',
                                        'inline' => 'table',
                                        'sortable' => 'id',))
        ->add('bFoto','file',array('label'=>'Обложка',
            'data_class'=> null,
            'required'=>false,
            ))
    ;
}

// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        //->add('author', null , array('label' => 'Автор',
          //                          'class' => 'AppBundle\Entity\BookAuthor',
            //                        ))
        //->add('heading', null, array('label' => 'Рубрика',
                                        //'class' => 'AppBundle\Entity\BookHeading',
                                        //))
    ;
}

// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('b_id', null, array('label' => 'ID'))
        ->add('bTitle', null , array('label' => 'Название'))
    ;
}
} 

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您需要在实体BooksHeading和BooksAuthor中指明级联类型, 试试这个:

@ORM\ManyToOne(targetEntity="AppBundle\Entity\Heading", casccade="persist")