Symfony2:插入集合表单数据

时间:2013-06-16 23:37:16

标签: symfony symfony-forms symfony-components

UserCompany实体之间存在一对一的关系 在初始化(创建)用户公司时,userID应该作为外键绑定到公司用户字段。但不是那样,我得到这个错误信息:

  

财产" id"在课堂上不公开   "网站\ CompanyBundle \实体\用户&#34 ;.也许你应该创建   方法" setId()"?

为什么Symfony想要在此表单是关于公司实体时创建新用户,而用户实体只是一个应该提供用户ID的集合。

这是我的代码:

Company.php实体

namespace Website\CompanyBundle\Entity;

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

/**
 * @ORM\Entity(repositoryClass="Website\CompanyBundle\Entity\Repository\CompanyRepository")
 * @ORM\Table(name="company")
 */
class Company
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="User", inversedBy="company")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

}


CompanyType.php

class CompanyType extends AbstractType
{
    private $security;
    public function __construct(SecurityContext $security)
    {
        $this->security= $security;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $user = $this->securityContext->getToken()->getUser();

        $builder
        ->add('user', new UserType($security))
        ->add('company_name')
        ->add('company_address')
        ...
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Website\CompanyBundle\Entity\Company'
        ));
    }

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


UserRelationType.php

class UserRelationType extends AbstractType
{
    private $user;

    public function __construct(SecurityContext $security){
        $this->user = $security->getToken()->getUser();
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', 'hidden', array('data' => $this->user->getId()))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Website\CompanyBundle\Entity\User'
        ));
    }

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


User.php实体

namespace Website\CompanyBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     * @ORM\OneToOne(targetEntity="Company", mappedBy="user")
     */
    protected $company;
}

3 个答案:

答案 0 :(得分:2)

您可以在UserRelationType中将实体映射到表单。在保存时,尝试在用户实体上设置id。如果您需要选择现有用户,则必须指定data transformer或使用entity type

如果您想设置当前用户,最好在pre_persist

等事件监听器中进行设置

答案 1 :(得分:2)

您实体中的属性受到保护,但您尚未为它们创建getter / setter。 (或者你还没有粘贴完整的代码)

因此,表单构建器无法访问用户的属性。

至少缺少User.php中的public function setId($id)

这就是抛出异常的原因:

Maybe you should create the method "setId()"?

使用...

为每个属性创建getter和settes
app/console doctrine:generate:entities

或手工制作......

user.php的

public function getId()
{
    return $this->id;
}

public function setId($id)
{
    $this->id = $id;
    return $this;
}

// ...

答案 2 :(得分:1)

我在没有添加集合的情况下解决了这个问题,只使用了本页所述的Doctrine传递持久性:https://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations

CompanyController.php

public function createIndex(Request $request){
    $user = $this->getId();
    $company = new Company();

    if($user instanceof User){
        $company->setUser($user);
    }

    $request = $this->getRequest();
    $createForm = $this->createForm(new CompanyType(), $company);

    if('POST' === $request->getMethod())
    {
        $createForm->bindRequest($request);

        if($createForm->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($company);
            $em->flush();
        }
    }
}


感谢大家的帮助