【问题标题】:Symfony 2 FOSUserBundle Relation to Products TableSymfony 2 FOSUserBundle 与产品表的关系
【发布时间】:2013-11-20 23:22:28
【问题描述】:

如果之前有人问过这个问题,我提前道歉。

我已成功设置 FOSUserBundle。

我正在尝试从文档中的该链接设置“http://symfony.com/doc/current/book/doctrine.html”产品示例。

我想做的是设置它,以便特定用户将产品添加到与该产品相关联的 FOSUser 的用户 ID。

Acme 文件夹中有两个包

Acme/UserBundle -> FOSUser 设置

Acme/MainBundle -> 我在其中设置产品。

我已经浏览了几次关系文件,但我在 Symfony2 中对它们不太清楚。

我认为这是基于 Products 表中的“user_id”的用户实体中的 OneToMany 关系,但我不是 100% 确定如何做到这一点。

有没有人知道关于我是否朝着正确的方向前进的教程甚至基本想法?非常感谢,一旦我到达那里,我显然会发布成功的结果。

【问题讨论】:

    标签: symfony doctrine-orm entity-relationship fosuserbundle


    【解决方案1】:

    您只需要在您的用户和您的产品之间添加一个关系。所以你的 Product 实体应该是这样的:

    src/Acme/MainBundle/Entity/Product.php

    namespace Acme\MainBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="product")
     */
    class Product
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $name;
    
        /**
         * @ORM\Column(type="decimal", scale=2)
         */
        protected $price;
    
        /**
         * @ORM\Column(type="text")
         */
        protected $description;
    
        /**
         * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="products")
         * @ORM\JoinColumn(name="user_id", nullable=false)
         */
        protected $user;
    }
    

    还有你的用户实体

    src/Acme/UserBundle/Entity/User.php

    namespace Acme\UserBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Acme\MainBundle\Entity\Product;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="user")
     */
    class User
    {
        /**-
         * User properties...
         */
    
        /**
         * @ORM\OneToMany(targetEntity="Acme\MainBundle\Entity\Product", mappedBy="user", cascade={"remove"})
         */
        protected $products;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->products = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        /**
         * Add product
         *
         * @param Product $product
         * @return Branch
         */
        public function addProduct(Product $product)
        {
            $this->products[] = $product;
    
            return $this;
        }
    
        /**
         * Remove product
         *
         * @param Product $products
         */
        public function removeProduct(Product $product)
        {
            $this->products->removeElement($product);
        }
    
        /**
         * Get products
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getProducts()
        {
            return $this->products;
        }
    }
    

    一切都在doctrine documentation中解释

    【讨论】:

      【解决方案2】:

      以下内容在 Symfony 2.3 中对我非常有效

      实体

      use Doctrine\ORM\Mapping as ORM;
      
      use Acme\UserBundle\Entity\User;
      
      /**
       * @ORM\Entity
       * @ORM\Table(name="products")
       */
      class Products
      {
          /**
           * @ORM\Column(type="integer")
           * @ORM\Id
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          protected $id;
      
          /**
           * @ORM\Column(type="string", length=100)
           */
          protected $name;
      
          /**
           * @ORM\Column(type="decimal", scale=2)
           */
          protected $price;
      
          /**
           * @ORM\Column(type="text")
           */
          protected $description;
      
      
          /**
           * Get id
           *
           * @return integer 
           */
          public function getId()
          {
              return $this->id;
          }
      
          /**
           * Set name
           *
           * @param string $name
           * @return Products
           */
          public function setName($name)
          {
              $this->name = $name;
      
              return $this;
          }
      
          /**
           * Get name
           *
           * @return string 
           */
          public function getName()
          {
              return $this->name;
          }
      
          /**
           * Set price
           *
           * @param float $price
           * @return Products
           */
          public function setPrice($price)
          {
              $this->price = $price;
      
              return $this;
          }
      
          /**
           * Get price
           *
           * @return float 
           */
          public function getPrice()
          {
              return $this->price;
          }
      
          /**
           * Set description
           *
           * @param string $description
           * @return Products
           */
          public function setDescription($description)
          {
              $this->description = $description;
      
              return $this;
          }
      
          /**
           * Get description
           *
           * @return string 
           */
          public function getDescription()
          {
              return $this->description;
          }
      
          /**
          * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", cascade={"remove"})
          * @ORM\JoinColumn(onDelete="CASCADE")
          */
         private $owner;
      
            public function getOwner()
          {
              return $this->owner;
          }
      
          public function setOwner(User $owner) 
          {
              $this->owner = $owner; 
          }
      }
      

      在控制器中

       /**
           * Creates a new Products entity.
           *
           */
          public function createAction(Request $request)
          {
              $entity = new Products();
              $form = $this->createCreateForm($entity);
              $form->handleRequest($request);
      
              if ($form->isValid()) {
                  $em = $this->getDoctrine()->getManager();
      
                 // Associate User Entity To Product 
                  $user = $this->container->get('security.context')->getToken()->getUser();
                  $entity->setOwner($user);
      
                  $em->persist($entity);
                  $em->flush();
      
                  return $this->redirect($this->generateUrl('products_show', array('id' => $entity->getId())));
              }
      
              return $this->render('AcmeMainBundle:Products:new.html.twig', array(
                  'entity' => $entity,
                  'form'   => $form->createView(),
              ));
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-26
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        相关资源
        最近更新 更多