我的symfony EntityBundle中有这些实体:第一个Organization.php
:
class Organization
{
/**
* @var integer
*
* @ORM'Column(name="organization_id", type="integer", nullable=false)
* @ORM'Id
* @ORM'GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @Assert'Type(
* type="string",
* message="The value {{ value }} is not a valid {{ type }}."
* )
* @Assert'NotNull()
* @Assert'NotBlank()
* @ORM'Column(name="organization_name", length=255)
*/
protected $name;
/**
* @var Address
*
* @ORM'OneToOne(targetEntity="Address", inversedBy="organization", cascade={"persist"})
* @ORM'JoinColumn(name="address_id", referencedColumnName="address_id")
*/
protected $address;
/**
* Set address
*
* @param 'MyNamespace'EntityBundle'Entity'Address $address
*
* @return Organization
*/
public function setAddress('MyNamespace'EntityBundle'Entity'Address $address = null)
{
$this->address = $address;
$address->setOrganization($this);
}
/**
* Get address
*
* @return 'MyNamespace'EntityBundle'Entity'Address
*/
public function getAddress()
{
return $this->address;
}
}
第二,Address.php
:
class Address
{
/**
* @var integer
*
* @ORM'Column(name="address_id", type="integer", nullable=false)
* @ORM'Id
* @ORM'GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @Assert'Length(
* max = 5,
* maxMessage = "Votre numéro de rue doit avoir maximum {{ limit }} chiffres"
* )
* @Assert'NotNull()
* @Assert'NotBlank()
* @ORM'Column(name="street_number", type="integer")
*/
protected $streetNumber;
/**
* @Assert'Type(
* type="string",
* message="The value {{ value }} is not a valid {{ type }}."
* )
* @Assert'NotNull()
* @Assert'NotBlank()
* @ORM'Column(name="street_name", length=255)
*/
protected $streetName;
/**
* @var 'Doctrine'Common'Collections'Collection
*
* @ORM'OneToOne(targetEntity="Organization", mappedBy="address")
*/
protected $organization;
/**
* Set organization
*
* @param 'MyNamespace'EntityBundle'Entity'Organization $organization
*
* @return Address
*/
public function setOrganization('MyNamespace'EntityBundle'Entity'Organization $organization = null)
{
$this->organization = $organization;
return $this;
}
/**
* Get organization
*
* @return 'MyNamespace'EntityBundle'Entity'Organization
*/
public function getOrganization()
{
return $this->organization;
}
/**
* Add organization
*
* @param 'MyNamespace'EntityBundle'Entity'Organization $organization
* @return Address
*/
public function addOrganization('MyNamespace'EntityBundle'Entity'Organization $organization)
{
$this->organization[] = $organization;
$organization->setOrganization($this);
return $this;
}
}
这是y控制器,以便以嵌入形式持久保存数据:
public function RegistrationAction()
{
$organization = new Organization();
$form = $this->createForm(new OrganizationType(), $organization)
->add('save', 'submit', array('label' => 'Create'));
$request = $this->getRequest();
if( $request->isMethod('POST') ) {
$form->bind($request);
if( $form->isValid() ) {
$em = $this->getDoctrine()->getManager();
$em->persist($organization);
$em->flush();
return $this->redirectToRoute('homepage');
}
}
return $this->render('MyBundle:MyFolder:Registration.html.twig', array(
'form' => $form->createView(),
));
}
这是我的表格类型OrganizationType.php
:
$builder
->add('name')
/* ... the other fields ... */
->add('address', 'collection', array(
'type' => new AddressType(),
'allow_add' => true,
'allow_delete' => false,
'by_reference' => false,
'mapped' => true,
));
}
最后是我的树枝视图来呈现形式:
<div>
{{ form_start(form, {'action': path('path_action'), 'method': 'POST'}) }}
{{ form_errors(form) }}
{{ form_row(form.name) }}
{# .. the other fields ...#}
{{ form_row(form.address.vars.prototype) }}
{{ form_end(form) }}
</div>
除了我提交表格外,一切都很顺利。发生此错误:
可捕获的致命错误:参数1传递给MyNamespace''EntityBundle''Entity''Organization::setAddress()必须是MyNamespace''EntityBundle''Entity''Address的实例,给定数组,调入C: ''wamp''www''myApp''vendor''symfony''symfony ''src''symfony''Component''PropertyAccess''PropertyActor.php在线502上并且被定义,'C:''wamp''www''myApp''src''MyNamespace''EntityBundle''Entity''Organization.php',"336",中的数组('this'=>对象(组织))src''MyNamespace''EntityBundle''Entity''Organization.php,第336行
我该怎么解决?
问题在这里
->add('address', 'collection', array(
'type' => new AddressType(),
'allow_add' => true,
'allow_delete' => false,
'by_reference' => false,
'mapped' => true,
));
即使是oneToOne关系,您也将表单属性类型设置为集合,因此当symfony解析表单数据时,它会将地址封装在数组中,您可以通过以下方式解决此问题:
->add('address', new AddressType());