I
I
Ilya Loopashko2020-09-28 14:03:56
symfony
Ilya Loopashko, 2020-09-28 14:03:56

When adding content, nothing happens, how to figure it out?

I am just learning Symfony and Easy Admin. There is an admin panel, you can create a post in it, with a category and a tag, the problem is that the tag is not added, I choose, I press save, it reboots and the selected tag is not added.

I select the Drum tag I
5f71c25dadb05132297888.png

press "Save and continue editing" the page is reloaded and the selected tag is not saved.
5f71c221dba5e970863336.png

Code from DefaultController.php

<?php

namespace App\Controller;

use App\Entity\Category;
use App\Entity\Menu;
use App\Entity\Post;
use App\Entity\Tag;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
    /**
     * @Route("/default", name="default")
     */
    public function index()
    {
        $em = $this->getDoctrine()->getManager();
        $post = $em->getRepository(Post::class)->findAll();
        $menu = $em->getRepository(Menu::class)->findAll();
        $category = $em->getRepository(Category::class)->findAll();
        $tag = $em->getRepository(Tag::class)->findAll();


        return $this->render('default/index.html.twig', [
            'controller_name' => 'DefaultController',
            'post'=>$post,
            'category'=>$category,
            'tag'=>$tag,
            'menu'=>$menu

        ]);
    }

    /**
     * @Route("/post/single/{post}", name="single")
     */
    public function single(Post $post)
    {
        return $this->render('default/single.html.twig', [
            'post'=>$post

        ]);
    }
}


Code from Entity\Tag.php
<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

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

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

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

    /**
     * @ORM\ManyToMany(targetEntity="Post", inversedBy="tags")
     */
    private $posts;

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

    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 getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * @return Collection|Post[]
     */
    public function getPosts(): Collection
    {
        return $this->posts;
    }

    public function addPost(Post $post): self
    {
        if (!$this->posts->contains($post)) {
            $this->posts[] = $post;
        }

        return $this;
    }

    public function removePost(Post $post): self
    {
        if ($this->posts->contains($post)) {
            $this->posts->removeElement($post);
        }

        return $this;
    }

    public function __toString()
    {
        return $this->name;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Loopashko, 2020-10-04
@deadloop

With the help of BoShurik . You need to add FormType with value 'by_reference' => false.

AssociationField::new('tags')->setFormTypeOptions([
                'by_reference' => false,
            ]),

A
Anton R., 2020-09-28
@anton_reut

Here, as in a car, there are two options - either there is nothing to burn, or there is nothing to set fire to. And either the code does not work for you, or the base is a curve.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question