我是symfony2的新手,我想创建一个表单。字段的值应基于用户数据。
我有n:m-relation的项目和用户,现在我想在twig中嵌入一个表单,我可以分配用户(多次点击一次)和项目。我的用户类看起来像:
// 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;
项目类看起来像:
// 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以及有关章节"How to dynamically Generate Forms Based on user Data"
的文档使用我的代码我总是得到错误“Class Pso \ ProjectBundle \ Form \ EntityRepository不存在”。我想我没有得到正确的文件。我不想从ProjectRepository中获取数据。这存在,但命名空间是Pso \ ProjectBundle \ Form。 但我的意图是在EventListener中使用querybuilder获取数据。每个tipp都会受到赞赏。
答案 0 :(得分:2)
在ProjectuserType类中添加以下行:use Doctrine\ORM\EntityRepository
希望它有用。 最好的问候。