Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

SS3 datamodel relation filters


Reply

8 Posts   1732 Views

Avatar
spierala

1 August 2012 at 2:12am Community Member, 80 Posts

Hello all,
I just try to figure out the SS3 Relation filters as described here:
http://doc.silverstripe.org/framework/en/topics/datamodel

There is one example which does not seem to work:

$members = Member::get()->filter(array(
'Groups.Title:StartsWith' => array('A', 'B'),
));

It should return all members in groups whose names start with 'A' or 'B'... but it does not.

For sure in my database is a group with title starting with 'A': Administratoren

Has anyone already experience with that?
Kind regards,
Florian

Avatar
schellmax

18 August 2012 at 2:46am Community Member, 126 Posts

similar problem here.

given 2 classes:
Card many_many Tags
Tag belongs_many_many Cards

both classes have a Title field; there are multiple Card objects linked to a tag with Title 'test'

some 'relation filters' i tried (none of them working):

$cards = Card::get()->filter(array(
'Tags.Title' => 'test'
)); //throws an error

$cards = Card::get()->filter(array(
'Tags.Title' => array('test')
)); //throws an error

$cards = Card::get()->filter(array(
'Tags.Title:StartsWith' => array('test')
)); //no results returned

the only filter i got to work is the following:

$cards = Card::get()->filter(array(
'Tags.Title:ExactMatch' => 'test'
));

looks like this thing is rather buggy?

Avatar
Bolixe

22 August 2012 at 3:33am (Last edited: 22 August 2012 3:41am), Community Member, 17 Posts

Hello guys,

I was also trying to use this relation filtering. Then I found this post, I have tried like schellmax and nothing. I guess I am doing something wrong with the names. Because is the same structure as its describe here.

Dataobject dLab

static $many_many = array(

'Persons' => 'dPerson'

);

Dataobject dPerson

static $belongs_many_many= array(

'Labs' => 'dLab'

);

Relation table is name "dLab_Persons"

Then I use:

$children = dLab::get()->filter(array(

   'dPerson.firstname:ExactMatch' => 'Matthias'

));

Do you see something wrong??

Avatar
schellmax

22 August 2012 at 4:01am Community Member, 126 Posts

@Bolixe: in your case it should be

<pre>
$children = dLab::get()->filter(array(
'Persons.firstname:ExactMatch' =>'Matthias'
));
</pre>

note it's 'Persons.firstname', not 'dPerson.firstname'

but no info yet about the other filters...

Avatar
Baukez

22 August 2012 at 4:05am Community Member, 4 Posts

One easy way to find out if the query works, it to append ?showqueries=1 to your URL, and see if the filters are applies at all.

Anyway, I believe this feature is only working rather recently...

Avatar
Bolixe

22 August 2012 at 9:14pm Community Member, 17 Posts

Yeah, I tried with this code schellmax. But I think could be because of the classes I have.

dLab and dPersons which are related, extends to MyDataObject which I created it, and then this one extend to DataObject from the core.

So do you think could be because this MyDataObject class should be include in the filter of the query?? It came to my mind tonight in dreams hehehe.

Thanks for the answer

Avatar
schellmax

22 August 2012 at 9:35pm (Last edited: 22 August 2012 9:35pm), Community Member, 126 Posts

it's just been confirmed by 'UncleCheese' that relation filters aren't implemented yet. i'll post a comment in the docs

see: https://groups.google.com/d/topic/silverstripe-dev/XF2OoYrwEWo/discussion

Avatar
Bolixe

29 August 2012 at 10:51pm (Last edited: 29 August 2012 10:51pm), Community Member, 17 Posts

Ok I have read the post yesterday. And since I only want to filter one relation I want to do by my raw sql.

I have tried SQLQuery() and buildSQL() to put this in the DataObject. But all of them are deprecated.

Did you try to use it??