PHP Doctrine ORM and Symfony 4: Invalid argument supplied to foreach() on saving data

I was doing a tutorial about Symfony (2018) on youtube:
https://www.youtube.com/watch?v=kfiKn5c9l84&ab_channel=TraversyMedia

when I enter the route ../articles/save, it should save my data in the database table. The first time, it worked. However, when I changed the data to second article and tried to save, I get this error about foreach() (see image):

enter image description here

I don’t know the reason for that or how to disable the Cache.. (or ChachedReader)..

this is my Code to save an article:

<?php

    namespace AppController;

    use SymfonyComponentHttpFoundationResponse;
    use SymfonyComponentRoutingAnnotationRoute;
    use SensioBundleFrameworkExtraBundleConfigurationMethod;
    use SymfonyBundleFrameworkBundleControllerAbstractController;
    use AppEntityArticle;
    class ArticleController extends AbstractController {


        /**
         * @Route("/article", name="test_index")
         * @Method({"GET"})
         */
        public function index() {
            $articles = ["Article1", "Article2"];
            return $this->render('./articles/index.html.twig', array('articles' => $articles));
        }

        
    /**
     * @Route("/article/save")
     */
    public function save() {
            $entityManager = $this->getDoctrine()->getManager();

            $article = new Article();
            $article->setTitle("Article Two");
            $article->setBody("This is the body for article two");

            $entityManager->persist($article);

            $entityManager->flush();
            return new Response("Saved an article with the id of".$article->getId());
        }


    }

edit:this is my article entity:

<?php

namespace AppEntity;

use AppRepositoryArticleRepository;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=ArticleRepository::class)
 */
class Article
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;


    /**
     * @ORMColumn(type="text", length=100)
     */
    private $title;

    /**
     * @ORMColumn(type="text")
     */
    private $body;

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

    public function getTitle(): ?string
    {
        return $this->title;
    }
    public function setTitle($title): ?string
    {
        $this->title = $title;
        return $this->title;
    }

    public function getBody(): ?string
    {
        return $this->body;
    }

    public function setBody(string $body): self
    {
        $this->body = $body;

        return $this;
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *