dtoq41429 2018-08-21 06:08
浏览 10
已采纳

如何使用symfony中附加的用户创建对象

I'm trying to create a product that the current authenticated user created.

The user has a relationship with the product entity, i need to make a way for a user to create product with the user associated with it.

I'm following this tutorial, but it doesn't cover how to store a product with a current user

This is what i have so far

ProductController.php

  public function create(Request $request)
    {

      $category = new Category();
      $category->setName($request->get('category'));

      $user = new User();
      // how would i get the current user and set it to a product.
      $entityManager = $this->getDoctrine()->getManager();

      $product = new Product();
      $product->setName($request->get('title'));
      $product->setPrice($request->get('price'));
      $product->setDescription($request->get('description'));

      $product->setCategory($category);

      $entityManager->persist($category);
      $entityManager->persist($product);
      $entityManager->flush();


      return $this->redirectToRoute('products');

    }

Should i use this method or try something else ?

Entity\User.php

public function addProduct(Product $product): self
{
    if (!$this->products->contains($product)) {
        $this->products[] = $product;
        $product->setUser($this);
    }

    return $this;
}

Entity\Product.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 */
class Product
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=190)
     */
    private $name;

    /**
     * @ORM\Column(type="integer")
     */
    private $price;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="products")
     * @ORM\JoinColumn(nullable=false)
     */
    private $category;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="products")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getPrice(): ?int
    {
        return $this->price;
    }

    public function setPrice(int $price): self
    {
        $this->price = $price;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): self
    {
        $this->category = $category;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }
}
  • 写回答

1条回答 默认 最新

  • dpdrtj1075 2018-08-21 06:43
    关注

    As I can see, your relation works through addProduct() method, but in your controller, you don't call addProduct() anywhere.

    try following...

    $entityManager->persist($user) // you forgot to persist a new User
    $entityManager->persist($category);
    $entityManager->persist($product);
    $user->addProduct(product)
    $entityManager->flush(); // now try to flush...
    

    Offtopic and a bit constructive criticism

    Just by looking at your controller I assume you playing around with Symfony and in particular with doctrine. If it is so, consider following. If not then just ignore it ;)

    • rename Product->user to Product->createdBy This naming convention makes it more obvious.
    • For that case it's better to switch from a bidirectional relation (your current state) to a unidirectional (google for it, but in short -> just throw away inversedBy and mappedBy part in you Product <-> User relation )
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题