简体   繁体   中英

Doctrine, @oneToMany, but only with activated field

I have an entity for Company and an entity for Review. Company have multiple reviews.

When i call to company->getReviews() , i want that it return ONLY review that isValidated field is TRUE .

How can I do ? Which way is better ?

/**
 * @ORM\Table(name="Company")
 */
class Company
{

    /**
     * @ORM\OneToMany(targetEntity="MyAppBundle\Entity\Review", mappedBy="company")
     */
    protected $reviews;


    /**
     * Add reviews
     *
     * @param \ProSearch\ReviewBundle\Entity\Review $reviews
     * @return Company
     */
    public function addReview(\ProSearch\ReviewBundle\Entity\Review $reviews)
    {
        $this->reviews[] = $reviews;

        return $this;
    }

    /**
     * Remove reviews
     *
     * @param \ProSearch\ReviewBundle\Entity\Review $reviews
     */
    public function removeReview(\ProSearch\ReviewBundle\Entity\Review $reviews)
    {
        $this->reviews->removeElement($reviews);
    }

    /**
     * Get reviews
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getReviews()
    {
        return $this->reviews;
    }

}

And the review entity :

/**
 * @ORM\Table()
 * @ORM\Entity()
 */
class Review
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    protected $title;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     */
    protected $description;


    /**
    * @ORM\ManyToOne(targetEntity="MyAppBundle\Entity\Company")
    */
    protected $company;



    /**
     * @var boolean
     *
     * @ORM\Column(name="isValidated", type="boolean")
     */
    protected $isValidated;

}

The point is that company->getReviews() can be used by doctrine. You shoud create a new method :

public function getValidatedReviews() {

     $validatedReviews = array();

     foreach($this->getReviews() as $review {

         if($review->getIsValidated())
              $validatedReview[] = $review;
     }

     return $validatedReviews;
}

Assuming Company::$reviews is a Doctrine Collection .

You can use the filter method on the collection. This yields a new collection with only validated reviews.

$companyObj->getReviews()->filter(function($review) { 
    return $review->isValidated();
});

Seeing as you've tagged this with Symfony as well. I'd advice against modifying Company::getReviews() because it is used in forms when using collections for example. It could also be quite confusing when a other developer wants all reviews from a Company. Calling getReviews() won't return the results the developer expects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM