2014-06-18 61 views
0

我有一個1對n的關係,所以一個Brand有很多Cars。我想要做的只是創建一個Web窗體,其中來自兩個實體的所有字段都會顯示出來。要做到這一點,我創建了一個表單類型,但我認爲我做錯了什麼,因爲我試圖在樹枝中打印表單域時出錯。誰能告訴我我在哪裏做錯了?通過附加兩個實體無法生成一個網絡表單

錯誤:

Method "brand" for object "Symfony\Component\Form\FormView" does not exist in CarBrandBundle:Default:both.html.twig at line 1 

實體:

class BrandEntity 
{ 
    protected $name; 
    protected $origin; 
    //Followed by getters and setters 
/** 
* @ORM\ManyToOne(targetEntity="BrandEntity", inversedBy="car") 
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=false) 
* @var object $brand 
*/ 
protected $brand; 
} 

class CarEntity 
{ 
    protected $model; 
    protected $price; 
    //Followed by getters and setters 
/** 
* @ORM\OneToMany(targetEntity = "CarEntity", mappedBy = "brand") 
* @var object $car 
*/ 
protected $car; 

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

public function addCar(\Car\BrandBundle\Entity\CarEntity $car) 
{ 
    $this->car[] = $car; 

    return $this; 
} 

public function removeCar(\Car\BrandBundle\Entity\CarEntity $car) 
{ 
    $this->car->removeElement($car); 
} 
} 

表單類型:

namespace Car\BrandBundle\Form\Type; 

use Car\BrandBundle\Entity\BrandEntity; 
use Car\BrandBundle\Entity\CarEntity; 
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('brand', new BrandEntity()) 
      ->add('car', new CarEntity()) 
      ->add('button', 'submit', array('label' => 'Add')) 
     ; 
    } 

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

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

控制器:

namespace Car\BrandBundle\Controller; 

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

class BothController extends Controller 
{ 
    public function indexAction() 
    { 
     $form = $this->getFrom(); 

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

    private function getFrom() 
    { 
     return $this->createForm(new BothType(), null, 
       array('action' => $this->generateUrl('bothCreate'))); 
    } 
} 

嫩枝:

{{ form_row(form.brand.name) }} 
{{ form_row(form.brand.origin) }} 
{{ form_row(form.car.model) }} 
{{ form_row(form.car.price) }} 

回答

1

如果你想要一個「汽車形式」,你需要選擇一個品牌,那麼其他答案就可以。

如果你想要的是一個「品牌形式」,你可以在其中添加/編輯/刪除幾輛汽車,那麼你需要嵌入一個表格集合。

菜譜答案就在這裏:http://symfony.com/doc/current/cookbook/form/form_collections.html

爲了使含有的汽車形式(1-N的關係)的集合品牌的形式:

的表單類型將是這樣的:

的品牌型

// src/Acme/TaskBundle/Form/Type/BrandType.php 
//... 
class BrandType extends AbstractType 
{ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('description'); 

    $builder->add('cars', 'collection', array('type' => new CarType())); 
} 

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

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

汽車類型:

namespace Acme\CarBundle\Form\Car; 

//... 
class TagType extends AbstractType 
{ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('name'); 
} 

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

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

然後控制器和視圖就像在食譜。最終它非常強大和簡單。

+0

我做的事情更簡單[這裏](http:// stackoverflow.com/questions/24316726/using-two-entities-to-create-a-single-form)。 – BentCoder

0

的實體添加到您必須使用實體字段類型的表單:from the docs

$builder->add('users', 'entity', array(
    'class' => 'AcmeHelloBundle:User', 
    'property' => 'username', 
)); 

In this case, all User objects will be loaded from the database and rendered as either a select tag, a set or radio buttons or a series of checkboxes (this depends on the multiple and expanded values). If the entity object does not have a __toString() method the property option is needed.

還請張貼關係。

+0

你的例子只適用於一個字段'用戶名'。如果我有100個實體字段呢?我是否複製它100次以獲得所有字段?問題已在上面更新以顯示關係。 – BentCoder

+0

我做的事情更簡單[這裏](http://stackoverflow.com/questions/24316726/using-two-entities-to-create-a-single-form) – BentCoder

相關問題