我是Symfony 2 Framework的新手.我想形成实体ManyToOne关系的嵌入.我必须实体地址和地址类型
地址实体
namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Address
{
private $id;
private $line1;
private $city;
private $zip;
private $phone;
/**
* @var string $type
*
* @ORM\ManyToOne(targetEntity="Addresstype")
* @ORM\JoinColumn(name="address_type_id", referencedColumnName="id")
*/
private $type;
}
Addresstype实体
namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Addresstype
{
private $id;
private $title;
}
地址控制器
namespace Webmuch\ProductBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Webmuch\ProductBundle\Entity\Address;
use Webmuch\ProductBundle\Form\Addresstype;
/**
* Address controller.
*
* @Route("/address")
*/
class AddressController extends Controller
{
/**
* displays a form to create a new Address entity.
*
* @Route("/new", name="address_new")
* @Template()
*/
public function newAction()
{
$entity = new Address();
$form = $this->createForm(new Addresstype(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView()
);
}
}
表格部分
namespace Webmuch\ProductBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class Addresstype extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('line1')
->add('line2')
->add('state')
->add('city')
->add('zip')
->add('phone')
->add('type')
;
}
}
我花了整整一天坚持这一点,我已经尝试了很多东西,但我无法管理让它工作.
解决方法:
它涵盖在documentation中.
您为Addresstype创建了另一种表单类型(称为AddresstypeType?丑陋,但您选择了名称)并将 – > add(‘type’)替换为 – > add(‘type’,new AddresstypeType());.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。