KandeeHolly KandeeHolly - 5 months ago 14
PHP Question

Entity Render Doesn't Work

I'm trying to render a query in a template, but I get this error:

The type hint of parameter "commentoarticle" in method
"addCommentoArticle" in class "AppBundle\Entity\Article" is invalid.


I don't know what I have done wrong.

This is my entity, that I want to render:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* CommentoArticle
*
* @ORM\Table()
* @ORM\HasLifecycleCallbacks
* @ORM\Entity
*/
class CommentoArticle
{

/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

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

/**
* @var string
*
* @ORM\Column(name="testo", type="text")
*/
private $testo;

/**
* @var datetime
*
* @ORM\Column(name="data", type="datetime")
*/
public $data;

/**
*@ORM\ManyToOne(targetEntity="Article",inversedBy="commentoarticle")
* @ORM\JoinColumn(name="article_id",referencedColumnName="id")
*/
protected $article;

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

/**
* Set nome
*
* @param string $nome
*
* @return Commento
*/
public function setNome($nome)
{
$this->nome = $nome;

return $this;
}

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

/**
* Set testo
*
* @param string $testo
*
* @return Commento
*/
public function setTesto($testo)
{
$this->testo = $testo;

return $this;
}

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


/**
* Set data
*
* @param \DateTime $data
*
* @return Commento
*/
public function setData($data)
{
$this->data = $data;

return $this;
}

/**
* Get data
*
* @return \DateTime
*/
public function getData()
{
return $this->data;
}

/**
* Set article
*
* @param \AppBundle\Entity\Article $article
* @return CommentoArticle
*/
public function setArticle(\AppBundle\Entity\Article $article=null)
{
$this->article = $article;

return $this;
}

/**
* Get article
*
* @return \AppBundle\Entity\Article
*/
public function getArticle()
{
return $this->article;
}

}


That is link with this other entity:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
* Article
*
* @ORM\Table()
* @ORM\HasLifecycleCallbacks
* @ORM\Entity
*/
class Article
{

/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

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

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

/**
* @var string
*
* @ORM\Column(name="testo", type="text")
*/
private $testo;

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

/**
* @var string $image
* @Assert\File( maxSize = "1024k", mimeTypesMessage = "Perfavore inserisci un'immagine valida!")
* @ORM\Column(name="image", type="string", length=255, nullable=true)
*/
private $image;

/**
* @var date
*
* @ORM\Column(name="data", type="date")
*/
public $data;

/**
* @ORM\OneToMany(targetEntity="CommentoArticle", mappedBy="article")
*/
protected $commentoarticle;

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

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

/**
* Set titolo
*
* @param string $titolo
*
* @return Article
*/
public function setTitolo($titolo)
{
$this->titolo = $titolo;

return $this;
}

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

/**
* Set autore
*
* @param string $autore
*
* @return Article
*/
public function setAutore($autore)
{
$this->autore = $autore;

return $this;
}

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

/**
* Set testo
*
* @param string $testo
*
* @return Article
*/
public function setTesto($testo)
{
$this->testo = $testo;

return $this;
}

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


/**
* Set image
*
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}

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

public function getFullImagePath() {
return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
}

protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return $this->getTmpUploadRootDir().$this->getId()."/";
}

protected function getTmpUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../web/imgArticoli/';
}

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
$this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
}
$this->setImage($this->image->getClientOriginalName());
}

/**
* @ORM\PostPersist()
*/
public function moveImage()
{
if (null === $this->image) {
return;
}
if(!is_dir($this->getUploadRootDir())){
mkdir($this->getUploadRootDir());
}
copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
unlink($this->getTmpUploadRootDir().$this->image);
}




/**
* Set data
*
* @param \DateTime $data
*
* @return Article
*/
public function setData($data)
{
$this->data = $data;

return $this;
}

/**
* Get data
*
* @return \DateTime
*/
public function getData()
{
return $this->data;
}

/**
* Set categoria
*
* @param string $categoria
*
* @return Article
*/
public function setCategoria($categoria)
{
$this->categoria = $categoria;

return $this;
}

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


/**
* Add commentoarticle
*
* @param \AppBundle\Entity\CommentoArticle $commentoarticle
*
* @return Article
*/
public function addCommentoArticle(\AppBundle\Entity\Commento $commentoarticle)
{
$this->commentoarticle[] = $commentoarticle;

return $this;
}

/**
* Remove commentoarticle
*
* @param \AppBundle\Entity\CommentoArticle $commentoarticle
*/
public function removeCommentoArticle(\AppBundle\Entity\CommentoArticle $commentoarticle)
{
$this->commentoarticle->removeElement($commentoarticle);
}

/**
* Get commentoarticle
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCommentoArticle()
{
return $this->commentoarticle;
}
}


In the controller I have my action:

public function commentiarticoliAction()/* ROTTA "commentiarticoli" */
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT ca
FROM AppBundle:CommentoArticle ca'
);

$repository = $query->getResult();

return $this->render('administration/commentiarticoli.html.twig', array(
'results' => $repository
));
}


Where is the mistake?

Answer

This is the problematic part:

/**
 * Add commentoarticle
 *
 * @param \AppBundle\Entity\CommentoArticle $commentoarticle
 *
 * @return Article
 */
public function addCommentoArticle(\AppBundle\Entity\Commento $commentoarticle)
{
    $this->commentoarticle[] = $commentoarticle;

    return $this;
}

Are you adding \AppBundle\Entity\CommentoArticle or \AppBundle\Entity\Commento?