我不知道要在数据库中插入一些数据时,这是什么问题,它显示此错误
<?php
namespace ArticleBundleEntity;
use DoctrineORMMapping as ORM;
/**
* ArticleRate
*
* @ORMTable(name="article_rate")
* @ORMEntity(repositoryClass="ArticleBundleRepositoryArticleRateRepository")
*/
class ArticleRate
{
/**
* @var DateTime
*
* @ORMColumn(name="date", type="date")
*/
private $date;
/**
* @var int
*
* @ORMId
* @ORMOneToOne(targetEntity="Article")
* @ORMJoinColumn(name="article_id", referencedColumnName="id")
*/
private $idArticle;
/**
* @var int
* @ORMId
* @ORMOneToOne(targetEntity="GUBundleEntityUtilisateur")
*
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
private $idUser;
public function __construct()
{
$this->date = new DateTime();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set date
*
* @param DateTime $date
*
* @return ArticleRate
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set idArticle
*
* @param integer $idArticle
*
* @return ArticleRate
*/
public function setIdArticle($idArticle)
{
$this->idArticle = $idArticle;
return $this;
}
/**
* Get idArticle
*
* @return int
*/
public function getIdArticle()
{
return $this->idArticle;
}
/**
* Set idUser
*
* @param string $idUser
*
* @return ArticleRate
*/
public function setIdUser($idUser)
{
$this->idUser = $idUser;
return $this;
}
/**
* Get idUser
*
* @return string
*/
public function getIdUser()
{
return $this->idUser;
}
}
/**
* @param $id
* @return JsonResponse
*/
public function AimeArticlesAction($id)
{
$em = $this->getDoctrine()->getManager();
$article=new ArticleRate();
$article->setIdArticle(1);
$article->setIdUser(1);
dump($article);
$article=(object) $article;
$em->persist($article);
$em->flush();
return new JsonResponse("yes");
}
}
我的错误对我来说似乎很清楚:您给出了一个整数(1(,而学说则期望对象。
学说是管理对象。这些意味着您应该使用$article->getUser()->getId()
而不是$article->getUserId()
。
对于您的特定问题:那两个功能期望对象
$article->setIdArticle(1);
$article->setIdUser(1);
这就是为什么您应该命名这些方法Article
和User
。您可以使用getReference
$article->setUser($this->getDoctrine()->getManager()->getReference(User:class, 1));
请阅读学说文档,在我看来,您远非良好实践