+-----------------+ +------------------------------------+
| item table | | description table |
+----+------+-----+ +----+---------+------+--------------+
| id | name | ... | | id | item_id | lang | text |
+----+------+-----+ +----+---------+------+--------------+
| 1 | 1st | ... | | 1 | 1 | en | english text |
+----+------+-----+ +----+---------+------+--------------+
| 2 | 2nd | ... | | 2 | 1 | de | deutsch text |
+----+------+-----+ +----+---------+------+--------------+
| 3 | 2 | en | english text |
+----+---------+------+--------------+
class Item {
...
/**
* @ORM\OneToMany(targetEntity="\Application\Entity\Desc", mappedBy="item")
*/
protected $description;
...
}
class Desc {
...
/**
* @ORM\ManyToOne(targetEntity="\Application\Entity\Item", inversedBy="description")
* @ORM\JoinColumn(name="item_id", referencedColumnName="id")
*/
protected $item;
/**
* @ORM\Column(name="text")
protected $text;
public function getDesc(/*passing lang*/)
{
//there returned array of values, not single result
return $this->text;
}
...
}我有两张OneToMany亲戚的桌子。我在实体中设置了@ORM注释标记,并成功地在item实体(EN和De lang)中获得了描述数组。如何通过在DB查询中将lang参数传递给描述实体,而不使用数组迭代和多余的SQL行来只获得一个lang描述?
发布于 2018-05-10 12:24:33
在Item类中,您可以这样做,这将只返回带有指定语言参数的描述:
/**
*
* @return \Application\Entity\Desc
*/
public function getDescByLanguage($language)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("lang", $language))
->setFirstResult(0)
->setMaxResults(1);
$descByLanguage = $this->description->matching($criteria);
return $descByLanguage[0];
}https://stackoverflow.com/questions/50253474
复制相似问题