我将表单类型嵌入到另一个这样的表单类型中
$builder->add('parent', new \Company\Bundle\Form\UserObjects\AParentType);
但是,当我尝试将请求绑定(bind)到表单时
if($request->getMethod() == 'POST') {
$form->bindRequest($request);
}
我得到了错误
似乎表单正在将“AParent”对象作为数组而不是作为实体传递。有任何想法吗?
编辑
User.php
<?php
// src/Company/Bundle/Entity/UserObjects/Users.php
namespace Company\Bundle\Entity\UserObjects;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Company\Bundle\Repository\UserObjects\UserRepository")
* @ORM\Table(name="user")
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $username;
/**
* @ORM\Column(type="string")
*/
protected $password;
/**
* @ORM\Column(type="string")
*/
protected $securityQuestion;
/**
* @ORM\Column(type="string")
*/
protected $securityAnswer;
/**
* @ORM\Column(type="string")
*/
protected $salt;
/**
* @ORM\OneToOne(targetEntity="AParent", inversedBy="user")
*/
private $parent;
public function serialize()
{
return serialize(array(
'username' => $this->getUsername(),
'password' => $this->getPassword(),
'salt' => $this->getSalt(),
'roles' => $this->getRoles(),
));
}
public function unserialize($serializedData)
{
$unserializedData = unserialize($serializedData);
$this->setUsername(isset($unserializedData['username']) ? $unserializedData['username'] : null);
$this->setPassword(isset($unserializedData['password']) ? $unserializedData['password'] : null);
$this->setSalt(isset($unserializedData['salt']) ? $unserializedData['salt'] : null);
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
return false;
}
public function equals(UserInterface $user)
{
if ($user->getUsername() != $this->getUsername()) {
return false;
}
if ($user->getEmail() != $this->getEmail()) {
return false;
}
return true;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
public function getUsername()
{
return $this->username;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Set password
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Set securityQuestion
*
* @param string $securityQuestion
*/
public function setSecurityQuestion($securityQuestion)
{
$this->securityQuestion = $securityQuestion;
}
/**
* Get securityQuestion
*
* @return string
*/
public function getSecurityQuestion()
{
return $this->securityQuestion;
}
/**
* Set securityAnswer
*
* @param string $securityAnswer
*/
public function setSecurityAnswer($securityAnswer)
{
$this->securityAnswer = $securityAnswer;
}
/**
* Get securityAnswer
*
* @return string
*/
public function getSecurityAnswer()
{
return $this->securityAnswer;
}
/**
* Set salt
*
* @param string $salt
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
/**
* Set parent
*
* @param Company\Bundle\Entity\UserObjects\AParent $parent
*/
public function setParent(\DABSquared\ProjectGradesBundle\Entity\UserObjects\AParent $parent)
{
$this->parent = $parent;
if($parent != null) {
$parent->setUser($this);
}
}
/**
* Get parent
*
* @return Company\Bundle\Entity\UserObjects\AParent
*/
public function getParent()
{
return $this->parent;
}
}
public function __construct()
{
}
}
AParent.php
<?php
// src/Company/Bundle/Entity/UserObjects/AParent.php
namespace Company\Bundle\Entity\UserObjects;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Company\Bundle\Repository\UserObjects\AParentRepository")
* @ORM\Table(name="parents")
* @ORM\HasLifecycleCallbacks()
*/
class AParent
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="parent")
*/
private $user;
/**
* @ORM\Column(type="string")
*/
protected $zipCode;
public function __construct()
{
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* @param Company\Bundle\Entity\UserObjects\User $user
*/
public function setUser(\Company\Bundle\Entity\UserObjects\User $user)
{
$this->user = $user;
}
/**
* Get user
*
* @return Company\Bundle\Entity\UserObjects\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set zipCode
*
* @param string $zipCode
*/
public function setZipCode($zipCode)
{
$this->zipCode = $zipCode;
}
/**
* Get zipCode
*
* @return string
*/
public function getZipCode()
{
return $this->zipCode;
}
}
UserType.php
<?php
namespace Company\Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('username',null,array('label' => 'Username:'))
->add('password',null,array('label' => 'Password:'))
->add('securityQuestion',null,array('label' => 'Security Question:'))
->add('securityAnswer',null,array('label' => 'Security Answer:'))
->add('parent', new \Company\Bundle\Form\UserObjects\AParentType);
}
public function getName()
{
return 'usertype';
}
}
AParentType.php
<?php
namespace Company\Bundle\Form\UserObjects;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class AParentType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('zipCode',null,array('label' => 'Zip Code:'));
}
public function getName()
{
return 'aparenttype';
}
}
最佳答案
您尚未在AParentType表单中设置数据类。您可以这样操作:
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Project\MyBundle\Entity\AParent',
);
}
对于更高版本的Symfony,您需要改用以下代码:
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Project\MyBundle\Entity\AParent',
));
}
替换为:
->add('parent', new \Company\Bundle\Form\UserObjects\AParentType);
有了这个:
->add('parent', new AParentType());
因为您已经包含了 namespace 。
如果是一对一的,则在表单模板中,您可以执行以下操作:
{{ form_row(form.parent.zipCode) }}
希望那应该工作。您的实体定义看起来不错:-)
关于php - Symfony 2嵌入式表格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8032498/