繁体   English   中英

实体渲染不起作用

[英]Entity Render Doesn't Work

我正在尝试在模板中呈现查询,但出现此错误:

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

我不知道我做错了什么。

这是我要渲染的实体:

<?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;
    }

}

这是与另一个实体的链接:

<?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;
    }
}

在控制器中,我可以执行以下操作:

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
    ));
}

错误在哪里?

这是有问题的部分:

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

    return $this;
}

您要添加\\AppBundle\\Entity\\CommentoArticle \\AppBundle\\Entity\\Commento\\AppBundle\\Entity\\Commento吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM