+-----------------+ +------------------------------------+
| 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注释标记,并在项目实体(EN和De lang)中成功获取描述数组。如何通过将lang参数传递给描述实体而不使用数组迭代和数据库查询中多余的SQL行来获取一个lang描述?
答案 0 :(得分:0)
在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];
}