Piotr Kowalczuk
Piotr Kowalczuk

Reputation: 399

Doctrine2 use of criteria inside the entity class

I try to write a method whose task would be to return only selected elements of the collection of items associated with a particular entity.

/**
 * @ORM\OneToMany(targetEntity="PlayerStats", mappedBy="summoner")
 * @ORM\OrderBy({"player_stat_summary_type" = "ASC"})
 */
protected $player_stats;

public function getPlayerStatsBySummaryType($summary_type)
{
    if ($this->player_stats->count() != 0) {
        $criteria = Criteria::create()
            ->where(Criteria::expr()->eq("player_stat_summary_type", $summary_type));

        return $this->player_stats->matching($criteria)->first();
    }

    return null;
}

but i get error:

PHP Fatal error:  Cannot access protected property Ranking\CoreBundle\Entity\PlayerStats::$player_stat_summary_type in /Users/piotrkowalczuk/Sites/lolranking/vendor/doctrine/common/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php on line 53

any idea how to fix this?

Upvotes: 1

Views: 3165

Answers (3)

Piotr Kowalczuk
Piotr Kowalczuk

Reputation: 399

Fixed. It should be:

    $criteria = Criteria::create()
        ->where(Criteria::expr()->eq("playerStatSummaryType", $summary_type));

Upvotes: 2

moonwave99
moonwave99

Reputation: 22820

Provide a getter for $player_stat_summary_type property in Ranking\CoreBundle\Entity\PlayerStats class.

Upvotes: 0

gremo
gremo

Reputation: 48477

Ensure that PlyerStats entity has getPlayerStatSummaryType() public method. It's being used by the @ORM\OrderBy annotation and (I suppose) by you custom criteria inside getPlayerStatsBySummaryType().

Upvotes: 1

Related Questions