Reputation: 1
I have searched google for hours now and I can't seem to find the answer i'm looking for. In Symfony2, I am attempting to use the Criteria::create() and filter on a season and its not working. Here's the full explanation:
I have 4 Entitys: User, UserPolicy, Role and Season. UserPolicy implements the RoleInterface and User implements the UserInterface.
User.php
class User implements UserInterface, \Serializable {
/**
* @OneToMany(targetEntity="UserPolicy", mappedBy="User", cascade={"persist", "remove"})
*/
private $Polocies;
}
UserPolicy.php
class UserPolicy Implements RoleInterface
{
/**
* @Id()
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ManyToOne(targetEntity="User", inversedBy="Polocies")
* @JoinColumn(name="userid", referencedColumnName="id")
*/
private $User;
/**
* @ManyToOne(targetEntity="Role")
* @JoinColumn(name="roleid", referencedColumnName="id")
*/
private $Role;
/**
* @ManyToOne(targetEntity="Season")
* @JoinColumn(name="seasonid", referencedColumnName="id")
*/
private $Season;
}
Role.php
class Role{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(name="role", type="string", length=50)
*/
private $Role;
/**
* @Column(name="label", type="string", length=50)
*/
private $Label;
/**
* @ManyToOne(targetEntity="Role")
* @JoinColumn(name="parentId", referencedColumnName="id", nullable=true)
*/
private $Parent;
}
Note the $Season variable on the UserPolicy.
Each year there will be a new season, and users will get a new set of policies, keeping the previous seasons policies.
In User.php, I need to call getRoles() which works and i can get the UserPolicies which have the roles attached to them and that works too.
The problem is introduced when i need to filter on the Seasons. I found the Criteria class:
$criteria = Criteria::create()
->where(Criteria::expr()->eq("Season", "1"));
But because Season is an object, I'm getting the error
Cannot match on AppBundle\Entity\UserPolicy::Season with a non-object value. Matching objects by id is not compatible with matching on an in-memory collection, which compares objects by reference.
How do I fetch the Season Object if i'm not supposed to be able to load other data in the User Entity? I don't know how to pass the Season Object. I even created a service to access the season object, but I can't access the get() function inside the Entity. HELP!
Upvotes: 0
Views: 1494
Reputation: 3016
I found this question which describes a similar issue to yours. It's not possible to use the criteria in the way you described.
So there are three solutions:
Fetch the entire collection and then filter it by season like in the question I mentioned earlier.
Create a repository method to fetch user policies by season id.
First fetch the season, and then use it in the default findBy()
method of the entity repository.
Hope this helps.
Upvotes: 1