簡體   English   中英

訪問細枝中表單類型中定義的變量

[英]Accessing variables defined in form type in twig

我不知道出了什么問題,但是樹枝無法訪問變量,並且Method "name, origin, car" for object "Symfony\\Component\\Form\\FormView" does not exist in CarBrandBundle:Default:both.html.twig at line all of them 我想念什么? 我相信這不僅是樹枝問題。

注意:這是一對一的關系,一個品牌可以擁有許多汽車。

品牌實體

namespace Car\BrandBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

class BrandEntity
{
    protected $id;
    protected $name;
    protected $origin;

    /**
     * @ORM\OneToMany(targetEntity = "CarEntity", mappedBy = "brand")
     * @var object $car
     */
    protected $car;

    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->car = new ArrayCollection();
    }
}

汽車實體

namespace Car\BrandBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

class CarEntity
{
    protected $id;
    protected $model;
    protected $price;

    /**
     * @ORM\ManyToOne(targetEntity="BrandEntity", inversedBy="car")
     * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
     * @var object $brand
     */
    protected $brand;
}

汽車形式

namespace Car\BrandBundle\Form\Type;

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

class CarType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction($options['action'])
            ->setMethod('POST')
            ->add('model', 'text', array('label' => 'Model'))
            ->add('price', 'text', array('label' => 'Price'))
            ->add('button', 'submit', array('label' => 'Add'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Car\BrandBundle\Entity\CarEntity')
        );
    }

    public function getName()
    {
        return 'car';
    }
}

兩種形式

namespace Car\BrandBundle\Form\Type;

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

class BothType extends AbstractType
{
    public function builder(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction($options['action'])
            ->setMethod('POST')
            ->add('name', 'text', array('label' => 'Name'))
            ->add('origin', 'text', array('label' => 'Origin'))
            ->add('car', 'collection', array('type' => new CarType()))
            ->add('button', 'submit', array('label' => 'Add'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Car\BrandBundle\Entity\BrandEntity',
            'cascade_validation' => true
        ));
    }

    public function getName()
    {
        return 'both';
    }
}

控制器

namespace Car\BrandBundle\Controller;

use Car\BrandBundle\Entity\BrandEntity;
use Car\BrandBundle\Form\Type\BothType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BothController extends Controller
{
    public function indexAction()
    {
        $form = $this->createForm(new BothType(), new BrandEntity(),
                array('action' => $this->generateUrl('bothCreate')));

        return $this->render('CarBrandBundle:Default:both.html.twig',
                array('page' => 'Both', 'form' => $form->createView()));
    }
}

枝條

{{ form_label(form.name) }}
{{ form_widget(form.name) }}

{{ form_label(form.origin) }}
{{ form_widget(form.origin) }}


{% for c in form.car %}
   {{ form_row(c.model) }}
{% endfor %}

您的問題出在您的實體上。 您甚至需要具有受保護屬性的getter和setters。 例如:

protected $name;
    /**
     * Set name
     *
     * @param string $name
     * @return Brand
     */
    public function setName($name)
    {
        $this->source = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM