-1

I'm facing a problem with persisting OneToMany relationship data into the database. Following is the Code:

Item Type

->add('title')
->add('image',FileType::class)
->add('size', CollectionType::class,[
    'entry_type'=> SizeType::class,
    'entry_options' => ['label' => false],
    'by_reference' => false,
    'allow_add' =>true,
    'allow_delete' => true
])
->add('content',CKEditorType::class)
->add('save', SubmitType::class);

Item Entity

class Item
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\OneToMany(targetEntity=Size::class, mappedBy="size",cascade={"persist"})
     */
    private $size;

    public function __construct()
    {
        $this->size = new ArrayCollection();
    }

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

    /**
     * @return Collection|Size[]
     */
    public function getSize(): Collection
    {
        return $this->size;
    }

    public function addSize(Size $size): self
    {
        if (!$this->size->contains($size)) {
            $this->size[] = $size;
            $size->setSize($this);
        }

        return $this;
    }

    public function removeSize(Size $size): self
    {
        if ($this->size->removeElement($size)) {
            // set the owning side to null (unless already changed)
            if ($size->getSize() === $this) {
                $size->setSize(null);
            }
        }

        return $this;
    }
}

Size Entity

class Size
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=Item::class, inversedBy="size", fetch="EAGER")
     */
    private $size;

    public function getSize(): ?Item
    {
        return $this->size;
    }

    public function setSize(?Item $size): self
    {
        $this->size = $size;

        return $this;
    }
}

Controller

/**
 * @Route("/admin/post-product", name="post_product")
 */
public function postProduct(Request $request, SluggerInterface $slugger): Response
{
    $item = new Item();   
    $size = new Size();
    $form = $this->createForm(ItemType::class, $item);
    $form->handleRequest($request);
    if($form->isSubmitted()){
        $file_image = $form->get('image')->getData();

        dump($item->getSize());
        //die;
        if($file_image){
           //image
        }

        $sizes = $item->getSize();
        $item->addSize($sizes);

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

        return $this->redirectToRoute('post_product');
    }
    return $this->render('admin/post-product.html.twig',[

        'form' => $form->createView(),
    ]);
} 

My problem here is I'm not able to save the data into database I have tried with loops ,but I couldn't. The class Item is onetomany with Size. Which is Item has related sizes(s,m,l,xl,xxl). What I want o achieve here is that when I click the save button the item and the size table must be populated. Please help me..

Julien B.
  • 3,023
  • 2
  • 18
  • 33
  • 1
    Welcome to Stack Overflow. When asking for debugging help, we ask that you condense your code by making a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). This is helpful for people to understand your problem without having to go through the whole code. – fjplaurr Dec 17 '20 at 19:28
  • Just to follow up on what @fjplaurr suggested, use the Edit link on the lower left corner of your question and get rid of the posted code except for the size related stuff. As the question currently stands, there is just too much irrelevant noise to wade though. I suspect Size::setItem is never being called but it's just a guess and I'm too lazy to copy/paste your code into an IDE to verify. – Cerad Dec 18 '20 at 12:42

1 Answers1

-1

You are trying to persist the relation from the inverse side (the one that has a mappedBy). Doctrine will not check the relation when persisting the inverse side. In this particular case, I think you mixed up the owning/inverse sides. I would try to put the inversedBy in your Item class and the mappedBy in the Size class.

Should you want to keep the relationship as is. You would have to set the property on the owning side when setting the property on the inverse side.

Julien B.
  • 3,023
  • 2
  • 18
  • 33