Symfony2-Doctrine2プロジェクトにgetDislike()関数を持つLikeエンティティがあります。Likeエンティティを取得し、エコーでgetDislike()関数を呼び出すと、何も出力されません。
echo '<br />'. $like->getDislike(); //Nothing is printed (1, 0, true, false)
echo '<br />'. $like->getId(); // the entity id is printed
if条件で同じエンティティを使用して同じ関数を呼び出すと、
if($like->getDislike() == FALSE){ //throw an exception
....}
次に、次のエラーが返されます。
A new entity was found through the relationship 'Acme\ArticleBundle\Entity\Article#likes' that
was not configured to cascade persist operations for entity: Acme\ArticleBundle\Entity
\Like@00000000653f908100000000749fcbfe. Explicitly persist the new entity or configure cascading
persist operations on the relationship. If you cannot find out which entity causes the problem
implement 'Acme\ArticleBundle\Entity\Like#__toString()' to get a clue.
上記のエラーが発生する理由!Articleエンティティは私のコードでは言及されていません(もちろん、教義スキームでは1つのArticleに多くのLikeとその逆の関係があります)。
何か案が?リクエストに応じて、Likeエンティティファイルを追加します。
namespace Acme\ArticleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\ArticleBundle\Entity\Like
*
* @ORM\Table()
* @ORM\Entity
*/
class Like
{
/**
* @ORM\ManyToOne(targetEntity="Article")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
*/
protected $article;
/**
* @ORM\Column(type="boolean")
*/
protected $dislike;
//.... other fields
/**
* Set dislike
*
* @param boolean $dislike
*/
public function setDislike($dislike)
{
$this->dislike = $dislike;
}
/**
* Get dislike
*
* @return boolean
*/
public function getDislike()
{
return $this->dislike;
}
//.... other methods
}
これが記事エンティティです:
namespace Acme\ArticleBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\ArticleBundle\Entity\Article
*
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\ArticleBundle\Repository\ArticleRepository")
*/
class Article
{
/**
*
* @ORM\OneToMany(targetEntity="Like", mappedBy="article")
*/
protected $likes;
//.... other fields
public function __construct() {
...
$this->likes = new \Doctrine\Common\Collections\ArrayCollection();
...
}
}
そしてここにコントローラーコードがあります:
public function likeDislikeItemAction(){
if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')){
$user = $this->get('security.context')->getToken()->getUser();
$entityType = "AcmeArticleBundle:Article"; // This static for debugging
$entityId = 1; // This static for debugging
$doctrine = $this->getDoctrine();
$item = $doctrine
->getRepository($entityType)
->findOneById($entityId);
if(is_object($item)){
$itemlikes = $item->getLikes();
$totvotes = sizeof($itemlikes);
$numlikes = $this->getNumLikes($itemlikes);
$numdislikes = $totvotes - $numlikes;
$dislike_value = 'like'; // This static for debugging
if($dislike_value == 'like') $isDislike = FALSE;
else $isDislike = TRUE;
$like = $this->getLikeDislikeEntity($doctrine, $user, $entityType, $entityId);
if(is_object($like)) {
$test = $like->getDislike();
echo 'test val ' . $test;
}
}
}
return NULL;
}
private function getLikeDislikeEntity($doctrine, $user, $entityType, $entityId){
if ($entityType == "AcmeArticleBundle:Article") {
return $doctrine->getRepository("AcmeArticleBundle:Like")
->findOneBy(array('author' => $user->getId(), 'article' =>$entityId));
}
return null;
}