Forms 从实体创建表窗体

Forms 从实体创建表窗体,forms,symfony,doctrine,Forms,Symfony,Doctrine,我有一个条令实体的列表(称为“电路””),我想生成一个表单,将它们列在一个列表中,并添加一种方法来勾选它们以进行大规模删除(类似于Sonata Admin所做的,不需要Admin类) 我到处找过了,但我一辈子都不知道该怎么办。该类只有一个层(普通的旧对象),每次我尝试向表单生成器添加集合类型时,都会出现以下错误: 属性“circuits”或方法“getCircuits()”、“circuits()”、“isCircuits()”、“hasCircuits()”、“\uuuu get()”、“\u

我有一个条令实体的列表(称为“电路””),我想生成一个表单,将它们列在一个列表中,并添加一种方法来勾选它们以进行大规模删除(类似于Sonata Admin所做的,不需要Admin类)

我到处找过了,但我一辈子都不知道该怎么办。该类只有一个层(普通的旧对象),每次我尝试向表单生成器添加集合类型时,都会出现以下错误:

属性“circuits”或方法“getCircuits()”、“circuits()”、“isCircuits()”、“hasCircuits()”、“\uuuu get()”、“\uuuu call()”中的任何一个都不存在,并且在类“NetDev\CoreBundle\Entity\Circuit”中具有公共访问权限

我应该创建一个“代理”类来创建电路集合吗?我错过什么了吗

到目前为止,我找到的所有howto都使用了一个“master”类,比如“Article”,以及一个子类集合,比如“Categories”,这不适用于我目前的问题

这是我的CircuitsController.php(我使用“addAction”进行测试,最终所有内容都将位于indexAction中):


正如@Cerad所说,这里的答案是通过

$listCircuits = $this->getDoctrine()->getManager()->getRepository('NetDevCoreBundle:Circuit')->findAll();

直接到集合。之后一切都很顺利。

您的“代理”类可以是一个数组,其中包含一个名为“circuits”的键,该键又包含一个circuit实体数组。将数组作为表单数据传递。@Cerad确实有效。我试图直接使用indexAction的->findAll()结果,但奇怪的是,它也起了作用,所以我将坚持使用它。谢谢你的帮助!如果您愿意,可以将其作为答案发布,这样我就可以将其标记为已解决:)
<?php

namespace NetDev\CoreBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CircuitType extends AbstractType
{
    /**                                                                                                                                                                                                                                        
     * @param FormBuilderInterface $builder                                                                                                                                                                                                    
     * @param array $options                                                                                                                                                                                                                   
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
          ->add('circuits', 'collection', array('type' => 'entity', 'allow_add' => true,
                                                'allow_delete' => true, 'by_reference' => false,
                                                'label' => false,
                                                'options' => array('class' => 'NetDevCoreBundle:Circuit',
                                                                   'label' => false, 'multiple' => true,
                                                                   'expanded' => true)
                                                ))
          /* ->add('vlanId', 'integer', array('required' => true, 'label' => 'VLAN ID')) */
          /* ->add('popOut', 'text', array('required' => true, 'label' => 'Injecting PoP', */
          /*                             'max_length' => 3)) */
          /* ->add('popsIn', 'textarea', array('required' => true, 'label' => 'Listening PoP')) */
          /* ->add('bandwidth', 'integer', array('label' => 'Bandwidth')) */
          /* ->add('xconnectId', 'text', array('label' => 'Cross-connect ID')) */
          /* ->add('Create', 'submit') */
        ;
    }

    /**                                                                                                                                                                                                                                        
     * @return string                                                                                                                                                                                                                          
     */
    public function getName()
    {
        return 'netdev_corebundle_circuit';
    }
}
<?php

namespace NetDev\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**                                                                                                                                                                                                                                            
 * Circuit                                                                                                                                                                                                                                     
 *                                                                                                                                                                                                                                             
 * @ORM\Table()                                                                                                                                                                                                                                
 * @ORM\Entity(repositoryClass="NetDev\CoreBundle\Entity\CircuitRepository")                                                                                                                                                                   
 */
class Circuit
{
    /**                                                                                                                                                                                                                                        
     * @var integer                                                                                                                                                                                                                            
     *                                                                                                                                                                                                                                         
     * @ORM\Column(name="id", type="integer")                                                                                                                                                                                                  
     * @ORM\Id                                                                                                                                                                                                                                 
     * @ORM\GeneratedValue(strategy="AUTO")                                                                                                                                                                                                    
     */
    private $id;

    /**                                                                                                                                                                                                                                        
     * @var integer                                                                                                                                                                                                                            
     *                                                                                                                                                                                                                                         
     * @ORM\Column(name="vlan_id", type="integer")                                                                                                                                                                                             
     * @Assert\Type(type="int")                                                                                                                                                                                                                
     * @Assert\Range(min="1", max="4096")                                                                                                                                                                                                      
     */
    private $vlanId;

    /**                                                                                                                                                                                                                                        
     * @var array                                                                                                                                                                                                                              
     *                                                                                                                                                                                                                                         
     * @ORM\Column(name="pop_out", type="array")                                                                                                                                                                                               
     * @Assert\NotBlank()                                                                                                                                                                                                                      
     * @Assert\Length(max=3)                                                                                                                                                                                                                   
     */
    private $popOut;

    /**                                                                                                                                                                                                                                        
     * @var array                                                                                                                                                                                                                              
     *                                                                                                                                                                                                                                         
     * @ORM\Column(name="pops_in", type="array")                                                                                                                                                                                               
     * @Assert\NotBlank()                                                                                                                                                                                                                      
     */
    private $popsIn;

    /**                                                                                                                                                                                                                                        
     * @var integer                                                                                                                                                                                                                            
     *                                                                                                                                                                                                                                         
     * @ORM\Column(name="bandwidth", type="integer")                                                                                                                                                                                           
     * @Assert\Type(type="int")                                                                                                                                                                                                                
     */
    private $bandwidth;

    /**                                                                                                                                                                                                                                        
     * @var string                                                                                                                                                                                                                             
     *                                                                                                                                                                                                                                         
     * @ORM\Column(name="xconnect_id", type="string", length=255)                                                                                                                                                                              
     * @Assert\NotBlank()                                                                                                                                                                                                                      
     * @Assert\Length(max="255")                                                                                                                                                                                                               
     */
    private $xconnectId;

    /* Getters and Setters stripped for clarity's sake */

    public function __toString() {
      return "{$this->vlanId}-{$this->popOut}";
    }
}
$listCircuits = $this->getDoctrine()->getManager()->getRepository('NetDevCoreBundle:Circuit')->findAll();