我试图用学说创建一个选择查询,但它没有用。我有三个表,产品(ID,名称),客户(ID,名称),订单(产品,客户)。我需要选择并显示客户下的所有订单,以及产品名称。如何使用教义进行此特定查询?对不起,如果这是一个平庸的问题......
产品:
<?php
namespace Example\Bundle\CrudBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*/
class Product
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $prize;
/**
* @var integer
*/
private $id;
/**
* Set name
*
* @param string $name
* @return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set prize
*
* @param string $prize
* @return Product
*/
public function setPrize($prize)
{
$this->prize= $prize;
return $this;
}
/**
* Get prize
*
* @return string
*/
public function getprize()
{
return $this->prize;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
客户:
<?php
namespace Example\Bundle\CrudBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Client
*/
class Client
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $age;
/**
* @var integer
*/
private $id;
/**
* Set name
*
* @param string $name
* @return Client
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set age
*
* @param string $age
* @return Client
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
/**
* Get age
*
* @return string
*/
public function getAge()
{
return $this->age;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
订单:
<?php
namespace Example\Bundle\CrudBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Orders
*/
class Orders
{
/**
* @var integer
*/
private $idProduct;
/**
* @var integer
*/
private $idIngrediente;
/**
* Set idProduct
*
* @param integer $idProduct
* @return Orders
*/
public function setIdProduct($idProduct)
{
$this->idProduct = $idProduct;
return $this;
}
/**
* Get idProduct
*
* @return integer
*/
public function getIdProduct()
{
return $this->idProduct;
}
/**
* Set idClient
*
* @param integer $idClient
* @return Orders
*/
public function setIdClient($idIngrediente)
{
$this->idClient= $idClient;
return $this;
}
/**
* Get idClient
*
* @return integer
*/
public function getIdClient()
{
return $this->idClient;
}
}
这就是我尝试进行查询的方式,首先检查产品是否在表中,然后从 Orders 和 Product 表中选择我需要的字段。
class ProductRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQuery(
'SELECT * FROM ExampleCrudBundle:Product p ORDER BY p.name ASC'
)
->getResult();
}
}
public function findOneByIdJoinedToCategory($id)
{
$query = $this->getEntityManager()
->createQuery('
SELECT p, c FROM ExampleCrudBundle:Product p
JOIN p.category c
WHERE p.id = :id'
)->setParameter('id', $id);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}