bas*_*ien 4 symfony doctrine-orm
量规和品牌表之间存在关系(许多量规到一个品牌).
这是我的代码
$dbChoices = $this->getConfigurationPool()
->getContainer()
->get('Doctrine')
->getManager()
->getRepository('RibambelMainBundle:RubricMenu')
->findBy(array('brand_id' => 2));
Run Code Online (Sandbox Code Playgroud)
每次运行此部件时,我都会收到以下错误:
无法识别的字段:brand_id
有谁可以帮我这个或者告诉我另一种方法吗?
class RubricMenu
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Title", type="string", length=255)
*/
private $title;
/**
* @var int
*
* @ORM\Column(name="position", type="integer")
*/
private $position;
/**
* @ORM\ManyToOne(targetEntity="Brand")
* @ORM\JoinColumn(name="brand_id", nullable=false)
*/
private $brand;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $title
*
* @return RubricMenu
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set position
*
* @param integer $position
*
* @return RubricMenu
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
/**
* Get position
*
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* Set brand
*
* @param \Ribambel\MainBundle\Entity\Brand $brand
*
* @return RubricMenu
*/
public function setBrand(\Ribambel\MainBundle\Entity\Brand $brand = null)
{
$this->brand = $brand;
return $this;
}
/**
* Get brand
*
* @return \Ribambel\MainBundle\Entity\Brand
*/
public function getBrand()
{
return $this->brand;
}
}
Run Code Online (Sandbox Code Playgroud)
根据您的实体,您应该执行以下操作.
$dbChoices = $this->getConfigurationPool()
->getContainer()
->get('Doctrine')
->getManager()
->getRepository('RibambelMainBundle:RubricMenu')
->findBy(array('brand' => 2));
Run Code Online (Sandbox Code Playgroud)
从对象的角度来看,外键搜索根本没有意义。brand_id不是您RubricMenu实体的字段,brand而是。
如果您有Brand实体,则可以像这样使用findBy(或magic方法findByBrand):(带有$myBrand的实例Brand)
$repository->findBy(['brand' => $myBrand]);
// OR
$repository->findByBrand($myBrand);
Run Code Online (Sandbox Code Playgroud)
如果仅具有目标实体的ID,则可以使用IDENTITYDQL函数,但在这种情况下,findBy由于它需要有效的字段名称,因此不能直接使用。因此,您将需要在RubricMenu存储库中添加一个方法。您的方法可能如下所示:
public function findByBrandId($brandId)
{
$qb = $this->createQueryBuilder('rm');
$qb->where('IDENTITY(rm.brand) = :brandId')
->setParameter('brandId', $brandId);
return $qb->getQuery()->getResult();
}
Run Code Online (Sandbox Code Playgroud)
顺便提一句,正如@lgkarolos的答案所指出的那样,
$repository->findBy(['brand' => $brandId]);
// OR
$repository->findByBrand($brandId);
Run Code Online (Sandbox Code Playgroud)
由于findBy方法内部将能够解析给定的参数是Brand实体的ID ,因此它也将起作用,因此它将产生与给定相应Brand实例相同的查询。
| 归档时间: |
|
| 查看次数: |
1743 次 |
| 最近记录: |