【问题标题】:Create a table form from Doctrine entities从 Doctrine 实体创建表格形式
【发布时间】:2023-03-27 12:26:01
【问题描述】:

我有一个 Doctrine 实体列表(称为“Circuit”),并希望生成一个表格,在

中列出它们并添加一种勾选方式它们用于批量删除(就像 Sonata Admin 所做的那样,不需要管理员类)。

我到处寻找,但我终其一生都不知道该怎么做。这个类只有一层(普通的旧对象),每次我尝试向表单构建器添加 collection 类型时,都会收到以下错误:

属性“电路”和“getCircuits()”、“电路()”、“isCircuits()”、“hasCircuits()”、“__get()”、“__call()”方法之一都不存在并在“NetDev\CoreBundle\Entity\Circuit”类中具有公共访问权限。

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

到目前为止,我发现的所有方法都使用“文章”等“主”类和“类别”等子类的集合,这不适用于我目前的问题。

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

<?php

namespace NetDev\WebManagerBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use NetDev\CoreBundle\Form\CircuitType;
use NetDev\CoreBundle\Entity\Circuit;

class CircuitsController extends Controller {
  public function indexAction($page = 1) {
    $listCircuits = $this->getDoctrine()->getManager()->getRepository('NetDevCoreBundle:Circuit')->findAll();
    $content = $this->get('templating')->render('NetDevWebManagerBundle:Circuits:index.html.twig',
                                                array('listCircuits' => $listCircuits));
    return new Response($content);
  }

  public function addAction(Request $request) {
    $circuit = new Circuit();
    $form = $this->createForm(new CircuitType(), $circuit);

    if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
      /* some action that is not actually relevant */
    }
    return new Response($this->get('templating')->render('NetDevWebManagerBundle:Circuits:add.html.twig',
                                                         array('circuit' => $circuit,
                                                               'form' => $form->createView())));
  }

CircuitType.php 文件:

<?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';
    }
}

最后是 Circuit.php 实体文件:

<?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}";
    }
}

如果您需要 twig 模板,请告诉我,我没有添加它,因为除了该异常之外,我什至还没有接近输出其他内容。

【问题讨论】:

  • 您的“代理”类可以只是一个数组,其键名为“电路”,该键又包含电路实体数组。将数组作为表单数据传递。
  • @Cerad 确实有效。我尝试直接使用 indexAction 的 ->findAll() 结果,奇怪的是它也有效,所以我会坚持下去。谢谢您的帮助 !如果您愿意,可以将其发布为答案,以便我可以将其标记为已解决:)

标签: forms symfony doctrine


【解决方案1】:

正如@Cerad 所说,这里的答案是传递结果

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

直接收藏。之后一切都很好。

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多