在持久化到数据库时,OneToOne和ManyToOne有什么区别?

时间:2019-09-24 11:30:47

标签: php entity symfony4 one-to-one relation

我是PHP Web开发的新手,正在使用Symfony4。我有一个实体产品,该产品与实体Photo具有OneToOne关系。在控制器中,当我创建两者之间的关系并将Product实体保存到数据库中时,照片也会自动上传。但是,对于与产品ManyToOne相关的其他实体,即使创建了该关系,我也必须将它们分别保留在控制器中。有人可以向我解释为什么会这样吗?

  public function createProductAction(EntityManagerInterface $em, Request $request) {


      $product = new Product();

      $photo = new Photo();


      $form = $this->createForm(NewProductForm::class);

      $form->handleRequest($request);

      if($form->isSubmitted() && $form->isValid())
      {

          $file = $request->files->get('new_product_form')['photo'];
          $directory = $this->getParameter('photos_directory');
          $filename = md5(uniqid()) . '.' . $file->guessExtension();

          $file->move(
            $directory,
            $filename
          );

          $data = $form->getData();
          $product->setName($data['name']);
          $product->setColor($data['color']);


          $photo->setName($filename);


          $product->setPhoto($photo);


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

          $em->flush();

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

      }



      return $this->render('productsPage/createProduct.html.twig', [

        'productTypeForm' => $form->createView()

      ]);

    }

    /**
     * @Route("/products/{slug}")
     */
    public function review($slug,EntityManagerInterface $em, Request $request) {

      $product = $this->getDoctrine()->getRepository(Product::class)->find($slug);
      $photo = $this->getDoctrine()->getRepository(Photo::class)->find($product->getPhoto()->getId());
      $comments = new Comments();

      $ratings = new Rating();

      $form = $this->createForm(ReviewProductForm::class);

      $form -> handleRequest($request);




      if($form->isSubmitted() && $form->isValid())
      {


        $data = $form ->getData();
        $comments->setComment($data['comment']);
        $ratings->setRating($data['rating']);

        // $product->addComment($comments);
        // $product->addRating($ratings);

        $comments->setProduct($product);
        $ratings->setProduct($product);

        $em = $this->getDoctrine()->getManager();


        //Flush the ratings to the DB so the value can be updated immedietly after review submit
        $em->persist($comments);
        $em->persist($ratings);
        $em->flush();

        //Get the avg value from the rating DB to set the products avg rating
        $avgRating = $this->getDoctrine()->getRepository(Rating::class)->avgRating($slug);
        $product->setAvgRat($avgRating[0][1]);

        //Flush the product to the DB
        $em->persist($product);
        $em->flush();

        $form = $this->createForm(ReviewProductForm::class);
        return $this->redirect($request->getUri());

      }





      return $this->render('productsPage/productReview.html.twig', [

        'form' => $form->createView(),
        'product' => $product,
        'photo' => $photo,
        'comments' => $product->getComments()

      ]);

    }

0 个答案:

没有答案
相关问题