-3

我有一个有帖子的博客。有一个字段 blog.creator = user.id。因此,有两个实体。我希望在 blog 表中创建条目时,将授权用户的 id 放在 blog.creator 字段中。

如何正确地做到这一点?

问题1:如果我自己创建一个表单并处理它,那么我想在表单验证上这样做:

class BlogController extends AbstractController
{
    private $security;
    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    public function createAction(Request $request)
    {
        $blog  = new Blog();
        $form    = $this->createForm(BlogType::class, $blog);
        $form->handleRequest($request);

        if ($form->isValid()) {

            $blog->setCreatorId($this->security->getUser()->getId());

            $em = $this->getDoctrine()
                ->getManager();
            $em->persist($blog);
            $em->flush();

这是对的吗?还是有一个神奇的解决方案?

问题 2:我安装了 EasyAdminBundle,它运行良好,可以轻松查看博客文章、编辑和添加它们。我应该如何配置以使该creator字段由id授权用户设置?我认为,答案应该写在https://symfony.com/doc/master/bundles/EasyAdminBundle/book/edit-new-configuration.html中,但由于某种原因在那里或其他部分没有找到任何东西。有一种感觉,这应该通过提供将传递给 createFormBuilder 的选项来完成:

# config/packages/easy_admin.yaml
easy_admin:
    entities:
        Customer:
            class: App\Entity\Customer
            form:
                form_options: { validation_groups: ['Default', 'my_validation_group'] }
    # ...
$form = $this->createFormBuilder($entity, array(
    'data_class' => 'App\Entity\Customer',
    'validation_groups' => array('Default', 'my_validation_group'),
))
-> ...

但是我必须在这些选项中设置什么?如果可能,请在文档中指向正确的位置。或者我应该在创建实体(在实体类中)时定义此字段的自动完成?但是然后我将实体与功能绑定,这似乎是不允许的

我的实体用户

namespace App\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="user")
 */
class User extends BaseUser implements UserInterface
{

博客

// src/Blogger/BlogBundle/Entity/Blog.php

namespace App\Blogger\BlogBundle\Entity;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\User;

/**
 * @ORM\Entity(repositoryClass="App\Blogger\BlogBundle\Entity\Repository\BlogRepository")
 * @ORM\Table(name="blog")
 * @ORM\HasLifecycleCallbacks
 */
class Blog
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $creator_id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
     */
    protected $user;
...
    /**
     * Set creator_id
     * @param integer $creator_id
     * @return Blog
     */
    public function setCreatorId($creator_id)
    {
        $this->creator_id = $creator_id;
        $this->setUser();
        return $this;
    }

    /**
     * Get creator_id
     * @return integer
     */
    public function getCreatorId()
    {
        return $this->creator_id;
    }

    /**
     * Set user.
     * @param integer $creator_id
     * @return Blog
     */
    private function setUser()
    {
        if(empty($this->creator_id)) {
            $this->user = new User();
        } elseif($this->em && !empty($this->creator_id)) {
            $this->user = $this->em->getRepository('User')->find($this->creator_id);
        }
        return $this;
    }

    /**
     * Get user.
     * @return \App\Entity\User|null
     */
    public function getUser()
    {
        return $this->user;
    }
    /**
     * @ORM\PostLoad
     * @ORM\PostPersist
     */
    public function fetchEntityManager(LifecycleEventArgs $args)
    {
        if(is_null($this->em)) {
            $this->em = $args->getEntityManager();
        }
        if(is_null($this->user)) {
            $this->setUser();
        }
    }
}
4

1 回答 1

0

好消息!最后,有人向我展示了文档 文档的正确位置。总是缺少详细的示例或将人戳到文档中的特定位置

现在一切都很简单:表 blog.creator_id 中的字段,实体中的属性:

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     */
    protected $creator;

控制器:

    public function createAction(Request $request)
    {
        $blog  = new Blog();
        $form    = $this->createForm(BlogType::class, $blog);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $blog->setCreator($this->getUser());

            $em = $this->getDoctrine()
                ->getManager();
            $em->persist($blog);
            $em->flush();

            return $this->redirect($this->generateUrl('BloggerBlogBundle_blog_show', array(
                    'id'    => $blog->getId()))
            );
        }

        return $this->render('@BloggerBlog/Blog/form.html.twig', array(
            'form'    => $form->createView()
        ));
    }
于 2019-09-26T14:59:55.883 回答