妖怪 / 多对多矩阵包
以矩阵形式渲染 Doctrine 多对多关系
v1.1
2016-05-31 10:13 UTC
Requires
- php: >=5.5.9
- symfony/framework-bundle: ~2.8|~3.0
- twig/twig: ~1.20|~2.0
This package is auto-updated.
Last update: 2024-08-25 11:40:43 UTC
README
此包旨在将 Doctrine 多对多关系以矩阵形式渲染到 Symfony 表单中。
安装
使用 Composer 将包作为依赖项添加
composer require yokai/many-to-many-matrix-bundle
启用包
<?php // config/bundles.php return [ // ... Yokai\ManyToManyMatrixBundle\YokaiManyToManyMatrixBundle::class => ['all' => true], ];
使用方法
让我们举一个例子:我们的应用程序通过 2 个 Doctrine 实体 User
和 Role
处理 Symfony 的安全。存在一个 Role
和 User
之间的多对多关系。
<?php namespace App\Entity; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'user')] class User { #[ORM\Column(type: 'integer')] #[ORM\Id] #[ORM\GeneratedValue] private ?int $id = null; #[ORM\Column(type: 'string', unique: true)] private string $email; /** * @var Collection<Role> */ #[ORM\ManyToMany(targetEntity: Role::class, inversedBy: 'users')] private Collection $roles; public function __toString(): string { return $this->email; } //... }
<?php namespace App\Entity; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'role')] class Role { #[ORM\Column(type: 'integer')] #[ORM\Id] #[ORM\GeneratedValue] private ?int $id = null; #[ORM\Column(type: 'string', unique: true)] private string $role; /** * @var Collection<User> */ #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'roles')] private Collection $users; public function __toString(): string { return $this->role; } //... }
我想创建一个表单矩阵来显示这些实体之间的关系。
<?php namespace App\Controller; use App\Entity\Role; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Yokai\ManyToManyMatrixBundle\Form\Type\ManyToManyMatrixType; class MatrixController extends AbstractController { #[Route(path: '/role-matrix', name: 'role-matrix')] public function roleMatrixAction(Request $request, EntityManagerInterface $manager): Response { $roles = $manager->getRepository(Role::class)->findAll(); $form = $this->createForm( ManyToManyMatrixType::class, $roles, [ 'class' => Role::class, 'association' => 'users', ] ); $form->handleRequest($request); if (!$form->isSubmitted() || !$form->isValid()) { return $this->render('role-matrix.html.twig', [ 'form' => $form->createView(), ]); } foreach ($roles as $role) { $manager->persist($role); } $manager->flush(); return $this->redirectToRoute('role-matrix'); } }
{% extends 'base.html.twig' %} {% block body %} {{ form_start(form) }} {{ form_widget(form) }} <button type="submit" class="btn btn-primary">update matrix</button> {{ form_end(form) }} {% endblock %}
重要提示
在上面的例子中,我们必须注意几个 重要 的东西。
拥有方
您 必须 与关联的 拥有方(即具有 ManyToMany 属性上 ìversedBy
特性的实体)一起工作
__toString
这两个实体 必须 有一个 __toString
方法来渲染标签
MIT 许可证
许可证可以在 此处 找到。
作者
此包最初由 Yann Eugoné 创建。请参阅 贡献者列表。