如果某个产品的数量等于1,我不想在索引页面中显示该产品。产品的数量存储在"数量"表中。表格数量链接到产品(ManyToOne)。
这就是我所做的。
public function product($id)
{
$query = $this->createQueryBuilder('e')
->leftJoin('e.quantity', 'r')
->where('r.quantity = :id')
->setParameter('id', $id)
->getQuery();
return $query->getResult();
}
数量
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="Post", cascade={"remove"})
* @ORMJoinColumn(name="product_id", referencedColumnName="id")
*/
protected $desk2;
/**
* @var integer
* @ORMColumn(name="quantity", type="integer", nullable=true)
*/
protected $quantity;
产品
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="telephone", type="string", length=30)
*/
private $telephone;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=125)
*/
private $name;
/**
* @var string
*
* @ORMColumn(name="description", type="text" , length=125)
*/
private $description;
指数
public function indexvAction($id)
{
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getManager();
$findEntities = $em->getRepository('FLYBookingsBundle:Post')->product($id);
$entity = $this->get('knp_paginator')->paginate($findEntities, $this->get('request')->query->get('page', 1), 9
);
if ($session->has('cart'))
$cart = $session->get('cart');
else
$cart = false;
return $this->render('FLYBookingsBundle:Post:indexv.html.twig', array('entity' => $entity,
'cart' => $cart));
}
编辑:
默认值:index.twitch
{% extends "::base.html.twig" %}
{% block newadvert %}
<div class="span4 offset1">
</div>
{{ render(controller('FLYBookingsBundle:Post:indexv')) }}
{% endblock newadvert %}
控制器:
public function indexAction()
{
return $this->render('FLYPlatformBundle:Default:index.html.twig');
}
这只会返回数量>1的产品(或您传递的任何数量参数)。
public function product($quantityLimit)
{
$query = $this->createQueryBuilder('e')
->join('e.quantity', 'r')
->addSelect('r')
->where('r.quantity > :quantityLimit')
->setParameter('quantityLimit', $quantityLimit)
->getQuery();
return $query->getResult();
}
如果你想退回所有产品,但只退回数量>1的产品,你会使用类似的leftJoin。
public function product($quantityLimit)
{
$query = $this->createQueryBuilder('e')
->leftJoin('e.quantity', 'r', 'WITH', 'r.quantity > :quantityLimit')
->addSelect('r')
->setParameter('quantityLimit', $quantityLimit)
->getQuery();
return $query->getResult();
}