Is it possible to use result of an SQL function as a field in Doctrine?

后端 未结 5 1612
失恋的感觉
失恋的感觉 2021-01-01 22:38

Assume I have Product entities and Review entities attached to products. Is it possible to attach a fields to a Product entity based o

5条回答
  •  忘掉有多难
    2021-01-01 23:23

    If you're on Doctrine 2.1+, consider using EXTRA_LAZY associations:

    They allow you to implement a method like yours in your entity, doing a straight count on the association instead of retrieving all the entities in it:

    /**
    * @ORM\OneToMany(targetEntity="Review", mappedBy="Product" fetch="EXTRA_LAZY")
    */
    private $Reviews;
    
    public function getReviewsCount() {
        return $this->Reviews->count();
    }
    

提交回复
热议问题