Zend3 Doctrine 实体传递参数

Zend3 Doctrine Entity passing parameters

+-----------------+    +------------------------------------+
|   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;
    }
   ...
}

我有两个 table 和 OneToMany 亲戚。我在实体中设置@ORM 注释标签并成功地在项目实体(EN 和 De lang)中获取描述数组。我如何通过将 lang 参数传递给描述实体而无需数组迭代和数据库查询中的多余 SQL 行来仅获取一个 lang 描述?

Item Class 中,您可以这样做,这将 return 只有具有指定语言参数的描述:

/**
 *
 * @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];
}