【发布时间】:2023-03-27 12:26:01
【问题描述】:
我有一个 Doctrine 实体列表(称为“Circuit”),并希望生成一个表格,在 我到处寻找,但我终其一生都不知道该怎么做。这个类只有一层(普通的旧对象),每次我尝试向表单构建器添加 collection 类型时,都会收到以下错误: 属性“电路”和“getCircuits()”、“电路()”、“isCircuits()”、“hasCircuits()”、“__get()”、“__call()”方法之一都不存在并在“NetDev\CoreBundle\Entity\Circuit”类中具有公共访问权限。 我应该创建一个“代理”类来创建电路集合吗?我错过了什么吗? 到目前为止,我发现的所有方法都使用“文章”等“主”类和“类别”等子类的集合,这不适用于我目前的问题。 这是我的 CircuitsController.php(我使用“addAction”进行测试,最终所有内容都将位于 indexAction 中): CircuitType.php 文件: 最后是 Circuit.php 实体文件: 如果您需要 twig 模板,请告诉我,我没有添加它,因为除了该异常之外,我什至还没有接近输出其他内容。 【问题讨论】: 中列出它们并添加一种勾选方式它们用于批量删除(就像 Sonata Admin 所做的那样,不需要管理员类)。
<?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())));
}
<?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}";
}
}