我有实体旅行,收藏和用户,每位用户都可以将旅行添加到他的收藏列表中。
当显示所有旅行的列表时,我想如果旅行已经在用户收藏夹的lis中,那么显示按钮(删除),如果不在列表中,那么显示(添加到收藏夹)。
在动作控制器中,我已经完成了一个queryBuilder,以获取当前经过身份验证的用户的所有收藏列表,并将结果放在($ favorite)中。
public function listAction($page, Request $request)
{
$em = $this->getDoctrine()->getManager();
// code of boutton add to or remove from favorite
$user = $this->getUser(); // this is a function to verify if user is AUTHENTICATED_REMEMBERED
if($user) {
$favorite = $em->getRepository('ProjectTravelBundle:Favorite')->getFavoriteByUser($user);
}
else{
$favorite = '';
}
$paginator = $this->get('knp_paginator');
$qb = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend();
$nb = $qb->getQuery()->getResult();
$pagination = $paginator->paginate(
$qb,
$request->query->get('page', $page),10);
return $this->render('ProjectFrontendBundle:Travel:travel-list-view.html.twig',array(
'pagination' => $pagination,
'nb' => $nb,
'favorite' =>$favorite, // list of favorites of current user
));
}
我考虑的解决方案是测试旅行是否在数组中(最喜欢的)所以显示boutton删除,如果没有,那么显示boutton添加,但我没有在 show.twig
{% for travel in pagination %}
//..................
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
{% if travel.id not in favorite %}
<span><a href="{{ path('frontend_travel_add_favorite', {'id': travel.id} ) }}" class="pull-right button btn-small red">Add to favorite</a></span>
{% else %}
<span><a href="{{ path('frontend_travel_delete_favorite', {'id': favorite.id} ) }}" class="pull-right button btn-small red">Remove from</a></span>
{% endif %}
// if user is not AUTHENTICATED_REMEMBERED
{% else %}
<span><a href="{{ path('frontend_travel_add_favorite', {'id': travel.id} ) }}" class="pull-right button btn-small red">Add to favorite</a></span>
{% endif %}
//..........
{% endfor %}
存储库:queryBuilder
// get list of favorite of current user
public function getFavoriteByUser($user)
{
$qb = $this->createQueryBuilder('f')
->Where('f.user = '.$user);
return $qb->getQuery()->getResult();
}
已更新
FavoriteEntity
class Favorite
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Project\UserBundle\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Project\TravelBundle\Entity\Travel", inversedBy="favorites")
* @ORM\JoinColumn(nullable=false)
*/
protected $travel;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* @param \Project\UserBundle\Entity\User $user
* @return Favorite
*/
public function setUser(\Project\UserBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \Project\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set travel
*
* @param \Project\TravelBundle\Entity\Travel $travel
* @return Favorite
*/
public function setTravel(\Project\TravelBundle\Entity\Travel $travel)
{
$this->travel = $travel;
return $this;
}
/**
* Get travel
*
* @return \Project\TravelBundle\Entity\Travel
*/
public function getTravel()
{
return $this->travel;
}
}
TravelEntity
class Travel
{
/....
/**
* @ORM\OneToMany(targetEntity="Project\TravelBundle\Entity\Favorite", mappedBy="travel", cascade={"remove"})
*/
private $favorites;
public function __construct()
{
$this->favorites = new \Doctrine\Common\Collections\ArrayCollection();
}
/...........
/**
* Add favorites
*
* @param \Project\TravelBundle\Entity\Favorite $favorites
* @return Travel
*/
public function addFavorite(\Project\TravelBundle\Entity\Favorite $favorites)
{
$this->favorites[] = $favorites;
$favorites->setTravel($this);
return $this;
}
/**
* Remove favorites
*
* @param \Project\TravelBundle\Entity\Favorite $favorites
*/
public function removeFavorite(\Project\TravelBundle\Entity\Favorite $favorites)
{
$this->favorites->removeElement($favorites);
}
/**
* Get favorites
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getFavorites()
{
return $this->favorites;
}
答案 0 :(得分:1)
你可以试试这个:
在您的控制器中:
if($user) {
$favorite = $em->getRepository('ProjectTravelBundle:Favorite')->getFavoriteByUser($user);
$ids = array_map(function($entity) { return $entity->getTravel()->getId(); }, $favorite);
} else{
$favorite = '';
$ids = array();
}
...
return $this->render('ProjectFrontendBundle:Travel:travel-list-view.html.twig',array(
'ids' => $ids,
'pagination' => $pagination,
'nb' => $nb,
'favorite' =>$favorite, // list of favorites of current user
));
在你的模板中:
{% if travel.id not in ids %}
<span><a href="{{ path('frontend_travel_add_favorite', {'id': travel.id} ) }}" class="pull-right button btn-small red">Add to favorite</a></span>
{% else %}
<span><a href="{{ path('frontend_travel_delete_favorite', {'id': favorite.id} ) }}" class="pull-right button btn-small red">Remove from</a></span>
{% endif %}
希望它有所帮助。