how to fix Array to string conversion error

how to fix Array to string conversion error

hello i was trying to add comment to data base after i filter bad words for example if i “write shit this blog is amazing” its added to data base as “**** this blog is amazing” so i found this on http://banbuilder.com i was trying to use it and here is my controller(it was working fine before i tried to filter)

public function detailsAction(Request $request,Blog $blog){
        $censor = new CensorWords;

        $user=$this->getUser();
        if($user==null)
            return $this->redirectToRoute('fos_user_security_login');

        $add_comment = new CommentaireBlog();
        $em = $this->getDoctrine()->getManager();

        $comments = $em->getRepository(CommentaireBlog::class)->findByBlog($blog);
        $add_comment->setBlog($blog);
        $add_comment->setUser($user);
        $add_comment->setDate( new DateTime());

        $form = $this->createFormBuilder($add_comment)

            ->add('contenu', TextareaType::class)

            ->getForm();

        $text = $form["contenue"]->getData();
        $string = $censor->censorString($text);
        $add_comment->setContenu($string);
        if ($request->getMethod() == 'POST') {
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getEntityManager();

                $em->persist($add_comment);
                $em->flush();

                return $this->redirectToRoute('blog_details', array('id' => $blog->getId()));
            }
        }


        $reportForm = $this->createForm('BlogBundleFormCommentReportForm');
        $reportForm->handleRequest($request);

        return $this->render('blog/details.html.twig', array(
            'form' => $form->createView(),
            'comment' => $add_comment,
            'blog' => $blog,
            'comments'=> $comments,

            'report_form' => $reportForm->createView(),
        ));


    }

im getting this error : Notice: Array to string conversion

i tried to locate which line is giving the error and it was this line

$add_comment->setContenu($string);

how can i fix this ? what should i change or add ?

here is the CommentaireBlog entity:

<?php

namespace BlogBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * CommentaireBlog
 *
 * @ORMTable(name="commentaire_blog")
 * @ORMEntity(repositoryClass="BlogBundleRepositoryCommentaireBlogRepository")
 */
class CommentaireBlog
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="contenu", type="string", length=255)
     */
    private $contenu;

    /**
     * @var DateTime
     *
     * @ORMColumn(name="date", type="datetime")
     */
    private $date;

    /**
     * @ORMManyToOne(targetEntity="Blog")
     */
    private $blog;

    /**
     * @ORMManyToOne(targetEntity="UserBundleEntityUser")
     */

    private $user;

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }

    /**
     * @return mixed
     */
    public function getBlog()
    {
        return $this->blog;
    }

    /**
     * @param mixed $blog
     */
    public function setBlog($blog)
    {
        $this->blog = $blog;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set contenu
     *
     * @param string $contenu
     *
     * @return CommentaireBlog
     */
    public function setContenu($contenu)
    {
        $this->contenu = $contenu;

        return $this;
    }

    /**
     * Get contenu
     *
     * @return string
     */
    public function getContenu()
    {
        return $this->contenu;
    }

    /**
     * Set date
     *
     * @param DateTime $date
     *
     * @return CommentaireBlog
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return DateTime
     */
    public function getDate()
    {
        return $this->date;
    }
}

Source: Symfony Questions

Leave a Reply

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