I’m having problems writing a correct DQL for my code (Exception : Invalid PathExpression. Must be a StateFieldPathExpression).
I’ll start by sketching the structure of my project.
Every entity inherits from IdentifiableObject that provides every entity with an id
/**
* @MappedSuperclass
*/
class IdentifiableObject {
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* getters and setters go here
*/
}
Then we have a User object that has a list of “UserProperties”
/**
* @Entity
* @Table(name="users")
*/
class User extends IdentifiableObject {
/**
* Other unimportant vars go here
*/
/**
* @Column(type="string")
*/
private $userName;
/**
* @OneToMany(targetEntity="UserProperty", mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $userProperties = null;
public function __construct() {
$this->userProperties = new ArrayCollection();
}
/**
* Getters and setters go here
*/
}
The Userproperty has a property (which is basicaly a type saying what kind of userproperty it is) and a value
/**
* @Entity
* @Table(name="userProperties")
*/
class UserProperty extends IdentifiableObject {
/**
* @OneToOne(targetEntity="Property")
* @JoinColumn(name="propertyID", referencedColumnName="id")
*/
private $property;
/**
* @ManyToOne(targetEntity="User")
* @JoinColumn(name="userID", referencedColumnName="id")
*/
private $userValue
}
And finally the Property
/**
* @Entity
* @Table(name="properties")
*/
class Property extends IdentifiableObject {
/**
* @Column(type="string")
*/
private $name;
/**
* @Column(type="string")
*/
private $description;
/**
* @Column(type="integer")
*/
private $mandatory = 0;
/**
* @OneToOne(targetEntity="PropertyType")
* @JoinColumn(name="type", referencedColumnName="id")
*/
private $type;
}
What i would like to do is get all Users that have a Userproperty corresponding to a certain Property.
My attempt was :
$queryUserId = "SELECT userprop.user FROM UserProperty userprop JOIN userprop.property prop WHERE prop.id = '$propertyId'";
or
$queryUserId = "SELECT user FROM User user JOIN user.userProperties userprop WHERE userprop in(SELECT up FROM UserProperty up WHERE up.property = '$property')";
As of today i’m not able to get something sensible out of the system other then “Invalid PathExpression. Must be a StateFieldPathExpression”…
Anyone can help?
Edit: I’ll clarify what i’m trying to do here.
What i want is the DQL version of this sql query (which works):
SELECT *
FROM users
WHERE id
IN (
SELECT u.userId
FROM userproperties u, properties p
WHERE u.propertyID = p.id
AND p.id =1
)
Where the p.id = xxx would be the property object (or at least the id) that i want to use in my search.
Ok seems i have found a working DQL solution:
Works! (You need to join on user in the subQuery else you get the dreaded “Invalid PathExpression” exception.