所以我没有问题,我不知道如何使用:
SELECT * FROM product WHERE nazwa = 'Sprite'
在symfony中。这是我来自"实体"的文件:
<?php
namespace MyMainBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping AS ORM;
/**
* @ORMEntity
* @ORMTable(name="product")
*/
class Product
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="integer", length=10)
*/
protected $cena;
/**
* @ORMColumn(type="integer", length=10)
*/
protected $ilosc;
/**
* @ORMColumn(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();
但是当我尝试使用它时,我会出现错误。有人知道怎么办?
$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();
尝试以下:
$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$query = $repository->createQueryBuilder('p');
$query->where('p.nazwa = :brand')
->setParameter('brand', 'Sprite');
$test = $query->getQuery()->getResult();