【问题标题】:insert many to many SonataAdmin Bundle插入多对多 SonataAdmin Bundle
【发布时间】:2013-11-28 21:55:54
【问题描述】:

我有两个表,一个叫做 Portal,另一个叫做 Categories,它们通过链接表连接,因为关系是多对多的。 代码表如下:

类分类

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

 /**
* @ManyToMany(targetEntity="Portal")
*/ 
 private $portal;

public function __construct() {
 $this->portal = new \Doctrine\Common\Collections\ArrayCollection();
}    
    ...

课堂门户

class Portal
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")     
*/

private $id;


/**
* @ManyToMany(targetEntity="Categories")
* @JoinTable(name="portal_categories")
*/
private $categories;


   public function __construct() {
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
  }
....

我开发了以下内容 在 PortalAdmin::configureFormFields

$formMapper
         ->with('Portal')   
         ->add('name', 'text', array('label' => ' Name'))
         ->add('description', 'text', array('label' => 'Description'))
         ->add('webSiteLink', 'text', array('label' => 'Web Site ','required'=> false))           
         ->add('categories', 'sonata_type_collection',array( 'by_reference' => false))
     ->end();

但是,只显示一个带有弹出窗口的按钮,用于插入新类别,但我需要,只显示一个包含类别的列表并添加任意数量的类别

当我尝试这样做时:

$formMapper
           ->with('Portal')   
                ->add('name', 'text', array('label' => ' Name'))
                ->add('description', 'text', array('label' => 'Description'))
                ->add('webSiteLink', 'text', array('label' => 'Web Site ','required'=> false))           
                ->add('categories', 'sonata_type_model', array('required' => false, 'expanded' => false, 'multiple' => true, 'label' => 'Chose your categories'))

            ->end();

显示以下错误: Project\PortalBundle\Entity\Categories 类的对象无法转换为字符串

【问题讨论】:

    标签: php symfony many-to-many sonata-admin


    【解决方案1】:

    可能对你有帮助。

    请更改两个实体的注释。

    实体门户

    /** 
     * @var \Categories
     * @ORM\ManyToMany(targetEntity="Categories")
     * @ORM\JoinTable(name="portal_Categories",
     *      joinColumns={@JoinColumn(name="portal_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="cat_id", referencedColumnName="id")}
     * )
     */
    private $categories;
    

    实体类别

     /**
     * @var \Portal
     *  
     * @ORM\ManyToMany(targetEntity="Portal", mappedBy="categories")
     */
    private $portals;
    

    我试试这个:

        $formMapper
           ->with('Portal')   
                ->add('name', 'text', array('label' => ' Name'))
                ->add('description', 'text', array('label' => 'Description'))
                ->add('webSiteLink', 'text', array('label' => 'Web Site ','required'=> false))           
                ->add('categories', 'sonata_type_model', array('required' => false, 'expanded' => false, 'multiple' => true, 'label' => 'Chose your categories'))
    
            ->end();
    

    可能会有帮助

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2017-05-04
      相关资源
      最近更新 更多