所以我没有什么问题,我不知道如何使用:
SELECT * FROM product WHERE nazwa = 'Sprite'
在Symfony。这是我的“实体”文件:
<?php
namespace My\MainBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="integer", length=10)
*/
protected $cena;
/**
* @ORM\Column(type="integer", length=10)
*/
protected $ilosc;
/**
* @ORM\Column(type="string", length=50)
*/
protected $nazwa;
public function getCena()
{
return $this->cena;
}
public function setCena($cena)
{
$this->cena = $cena;
}
public function getIlosc()
{
return $this->ilosc;
}
public function setIlosc($ilosc)
{
$this->ilosc = $ilosc;
}
public function getNazwa()
{
return $this->nazwa;
}
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
我试过这样的事情:
$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$query = $repository->createQueryBuilder('p')->select('p')->where('p.nazwa == Sprite')->getQuery();
$test = $query->getResult();
但是当我尝试使用它时,我收到了一个错误。有人知道什么是错的?
答案 0 :(得分:1)
$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$result = $repository->findByNazwa('Sprite');
或使用QueryBuilder
$query = $repository->createQueryBuilder('p')->select('p')->where('p.nazwa = :nazwa')->setParameter('nazwa', 'Sprite')->getQuery();
$test = $query->getResult();
答案 1 :(得分:1)
试试这个:
$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$query = $repository->createQueryBuilder('p');
$query->where('p.nazwa = :brand')
->setParameter('brand', 'Sprite');
$test = $query->getQuery()->getResult();