I have used the following query in Silverstripe 2.x projects:
$obj = DataObject::get_one('Post', "\"URLSegment\" = '$segment'")
Does anyone know how I would replicate this query in the new ORM?
I have tried :
$obj = Post::get()->filter("\"URLSegment\" = '$segment'")
$obj = Post::get()->where("\"URLSegment\" = '$segment'")
And neither appear to work.
Thanks!
The issue you’re having is that DataObject::get() will return a collection of objects, whereas DataObject::get_one() will return a single object of the specified class. The Silverstripe 3 equivalent is:
With this scenario (as with SS2) you would need to make sure you’re explicitly taking care escaping to avoid injection vulnerabilities. However the
filterfunction will now take care of that for you. So what you really want now is:The related documentation is at: http://doc.silverstripe.org/framework/en/topics/datamodel