Php 如何在Symfony 2.8中获取表单存储库中的用户对象
我正在Symfony上开发一个web应用程序。我有两个实体是关于“线”和“剂量计”的问题。所以,一个剂量计可以有很多条线。 现在我正在为Line实体实现CRUD,所以Line实体有一个包含数据库中所有剂量计的“下拉列表”。 问题是,下拉列表中的所有剂量计都来自任何用户,但我需要在这个下拉列表中只包含user_id=currentUser_id的选项 因此,我的控制器操作:Php 如何在Symfony 2.8中获取表单存储库中的用户对象,php,symfony,dql,Php,Symfony,Dql,我正在Symfony上开发一个web应用程序。我有两个实体是关于“线”和“剂量计”的问题。所以,一个剂量计可以有很多条线。 现在我正在为Line实体实现CRUD,所以Line实体有一个包含数据库中所有剂量计的“下拉列表”。 问题是,下拉列表中的所有剂量计都来自任何用户,但我需要在这个下拉列表中只包含user_id=currentUser_id的选项 因此,我的控制器操作: /** * @Route("/add-line", name="addLine") */ public function
/**
* @Route("/add-line", name="addLine")
*/
public function createLineAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$line = new Line();
$form = $this->createForm(LineType::class, $line);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$em->persist($line);
$em->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('AppBundle:default:formLines.html.twig', array(
'form' => $form->createView(),
));
}//create dossier action
我的线型(表单生成器)
My Line.php(实体)
在构建表单的控制器中,需要将用户对象传递给表单类型
$tokenStorage = $this->get('security.token_storage');
$form = $this->createForm(new LineType($tokenStorage), $line);
//... other stuff
现在,在您的表单中,输入receive这个tokenStorage对象,检索user对象并传递给repo函数
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//.. other use statements
class LineType extends AbstractType
{
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->user = $tokenStorage->getToken()->getUser();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$userId = $this->user->getId();
$builder
->add('name')->add('dosier')
->add('dosier', EntityType::class, array(
'class' => 'AppBundle:Dosier',
'query_builder' => function($repo) use($userId) {
return $repo->dosiersOfCurrentUser($userId);
},
'choice_label' => 'name',
))
->add('save', SubmitType::class, array(
'label' => 'Save',
'attr'=> array('class'=>'btn btn-success submitButton')
)
);
}
}
在回购中应用过滤器
class DosierRepository extends \Doctrine\ORM\EntityRepository
{
public function dosiersOfCurrentUser($userId) {
return $this->createQueryBuilder('dosier')
->where('dosier.userId = :userId ')
->setParameter('userId',$userId)
->orderBy('dosier.name', 'DESC');
}
}
谢谢你的回答。一切看起来都很好,除了那个错误:自动加载程序希望在文件“/home/computer/project/src/AppBundle/Form/LineType.php”中定义类“AppBundle\Form\LineType”。找到了文件,但类不在其中,类名或命名空间可能有输入错误。如果我把LineRepository或DosierRepository放在文件夹表单上,它会给我同样的错误,或者如果我在entity/Loan.php中更改目录地址。。。相同。@new_newB1e可能是您的名称空间或类名有问题。请看一看谷歌的类似名称。哦,对不起,我刚才注意到在这多个文件中。。。我的LineType.php没有名称空间语句。一切正常,谢谢你的帮助!
$tokenStorage = $this->get('security.token_storage');
$form = $this->createForm(new LineType($tokenStorage), $line);
//... other stuff
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//.. other use statements
class LineType extends AbstractType
{
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->user = $tokenStorage->getToken()->getUser();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$userId = $this->user->getId();
$builder
->add('name')->add('dosier')
->add('dosier', EntityType::class, array(
'class' => 'AppBundle:Dosier',
'query_builder' => function($repo) use($userId) {
return $repo->dosiersOfCurrentUser($userId);
},
'choice_label' => 'name',
))
->add('save', SubmitType::class, array(
'label' => 'Save',
'attr'=> array('class'=>'btn btn-success submitButton')
)
);
}
}
class DosierRepository extends \Doctrine\ORM\EntityRepository
{
public function dosiersOfCurrentUser($userId) {
return $this->createQueryBuilder('dosier')
->where('dosier.userId = :userId ')
->setParameter('userId',$userId)
->orderBy('dosier.name', 'DESC');
}
}