symfony2 forms 根据用户数据动态生成表单
symfony2 forms dynamically Generate Forms Based on user Data
我是 symfony2 的新手,我想创建一个表单。字段值应基于用户数据。
我的项目和用户具有 n:m 关系,现在我想在 twig 中嵌入一个表单,我可以在其中分配用户(单击多个)和项目。我的 class 用户看起来像:
// src/Pso/LogBundle/Entity/User.php
namespace Pso\LogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="users", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
* @ORM\Entity(repositoryClass="Pso\LogBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=32)
*/
private $salt;
/**
* @ORM\Column(type="string", length=250)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*
*/
private $roles;
项目的 class 看起来像:
// src/Pso/ProjectBundle/Entity/Project.php
namespace Pso\ProjectBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="project", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
* @ORM\Entity(repositoryClass="Pso\ProjectBundle\Entity\ProjectRepository")
*/
class Project
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true, nullable=false)
*/
private $project_nr;
/**
* @ORM\ManyToOne(targetEntity="Pso\ProjectBundle\Entity\Mandator", inversedBy="projects")
* @ORM\JoinColumn(name="mandator_id", referencedColumnName="id")
*/
private $mandator;
/**
* @ORM\Column(type="string", length=80, nullable=false)
*/
private $project_name;
/**
* @ORM\Column(type="string", length=50)
*/
private $customer;
/**
* @ORM\Column(type="string", length=50)
*/
private $label;
/**
* @ORM\Column(type="date")
*/
private $shipping_date;
/**
* @ORM\Column(type="float")
*/
private $advance_payment;
/**
* @ORM\Column(type="integer", length=1)
*/
private $probability;
/**
* @ORM\Column(type="blob")
*/
private $special_demand;
/**
* @ORM\Column(type="string", length=4)
*/
private $currency;
/**
* @ORM\Column(type="integer", length=1, nullable=false)
*/
private $status;
/**
* @ORM\Column(type="string", length=100)
*/
private $contract_nr;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbinsert;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbupdate;
/**
* @ORM\ManyToMany(targetEntity="Pso\LogBundle\Entity\User", cascade={"persist", "remove"})
*/
private $users;
public function __construct() {
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
我的 ProjectuserType 看起来像:
namespace Pso\ProjectBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
class ProjectuserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('project','text')
;
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Pso\LogBundle\Entity\User',
'property' => 'username',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('x')->FROM('PsoLogBundle:user', 'u')
->WHERE('u.id = :id')
->setParameter('id',5)
;
// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->findnotsignedusers();
},
);
// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->createOrderByFullNameQueryBuilder();
},
);
// create the field, this is similar the $builder->add()
// field name, field type, data, options
$form->add('user', 'entity', $formOptions);
}
);
}
public function getName()
{
return 'Projectuser';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null,
));
}
}
类型 I 面向 post and on documentation about chapter "How to dynamically Generate Forms Based on user Data"
使用我的代码,我总是得到错误 "Class Pso\ProjectBundle\Form\EntityRepository does not exist"。我想我没有得到正确的文档。我不想从我的 ProjectRepository 中获取数据。这存在,但名称空间是 Pso\ProjectBundle\Form。
但我的目的是在我的 EventListener 中使用查询生成器获取数据。我们将不胜感激。
在您的 ProjectuserType class 中添加此行:use Doctrine\ORM\EntityRepository
希望对您有所帮助。
最好的问候。
我是 symfony2 的新手,我想创建一个表单。字段值应基于用户数据。
我的项目和用户具有 n:m 关系,现在我想在 twig 中嵌入一个表单,我可以在其中分配用户(单击多个)和项目。我的 class 用户看起来像:
// src/Pso/LogBundle/Entity/User.php
namespace Pso\LogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="users", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
* @ORM\Entity(repositoryClass="Pso\LogBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=32)
*/
private $salt;
/**
* @ORM\Column(type="string", length=250)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*
*/
private $roles;
项目的 class 看起来像:
// src/Pso/ProjectBundle/Entity/Project.php
namespace Pso\ProjectBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="project", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
* @ORM\Entity(repositoryClass="Pso\ProjectBundle\Entity\ProjectRepository")
*/
class Project
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true, nullable=false)
*/
private $project_nr;
/**
* @ORM\ManyToOne(targetEntity="Pso\ProjectBundle\Entity\Mandator", inversedBy="projects")
* @ORM\JoinColumn(name="mandator_id", referencedColumnName="id")
*/
private $mandator;
/**
* @ORM\Column(type="string", length=80, nullable=false)
*/
private $project_name;
/**
* @ORM\Column(type="string", length=50)
*/
private $customer;
/**
* @ORM\Column(type="string", length=50)
*/
private $label;
/**
* @ORM\Column(type="date")
*/
private $shipping_date;
/**
* @ORM\Column(type="float")
*/
private $advance_payment;
/**
* @ORM\Column(type="integer", length=1)
*/
private $probability;
/**
* @ORM\Column(type="blob")
*/
private $special_demand;
/**
* @ORM\Column(type="string", length=4)
*/
private $currency;
/**
* @ORM\Column(type="integer", length=1, nullable=false)
*/
private $status;
/**
* @ORM\Column(type="string", length=100)
*/
private $contract_nr;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbinsert;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbupdate;
/**
* @ORM\ManyToMany(targetEntity="Pso\LogBundle\Entity\User", cascade={"persist", "remove"})
*/
private $users;
public function __construct() {
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
我的 ProjectuserType 看起来像:
namespace Pso\ProjectBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
class ProjectuserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('project','text')
;
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Pso\LogBundle\Entity\User',
'property' => 'username',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('x')->FROM('PsoLogBundle:user', 'u')
->WHERE('u.id = :id')
->setParameter('id',5)
;
// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->findnotsignedusers();
},
);
// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->createOrderByFullNameQueryBuilder();
},
);
// create the field, this is similar the $builder->add()
// field name, field type, data, options
$form->add('user', 'entity', $formOptions);
}
);
}
public function getName()
{
return 'Projectuser';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null,
));
}
}
类型 I 面向 post and on documentation about chapter "How to dynamically Generate Forms Based on user Data"
使用我的代码,我总是得到错误 "Class Pso\ProjectBundle\Form\EntityRepository does not exist"。我想我没有得到正确的文档。我不想从我的 ProjectRepository 中获取数据。这存在,但名称空间是 Pso\ProjectBundle\Form。 但我的目的是在我的 EventListener 中使用查询生成器获取数据。我们将不胜感激。
在您的 ProjectuserType class 中添加此行:use Doctrine\ORM\EntityRepository
希望对您有所帮助。 最好的问候。