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   1862 Views

Avatar
spierala

Community Member, 80 Posts

1 August 2012 at 2:12am

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

Community Member, 126 Posts

18 August 2012 at 2:46am

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

Community Member, 17 Posts

22 August 2012 at 3:33am

Edited: 22/08/2012 3:41am

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

Community Member, 126 Posts

22 August 2012 at 4:01am

@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

Community Member, 4 Posts

22 August 2012 at 4:05am

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

Community Member, 17 Posts

22 August 2012 at 9:14pm

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

Community Member, 126 Posts

22 August 2012 at 9:35pm

Edited: 22/08/2012 9:35pm

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

Community Member, 17 Posts

29 August 2012 at 10:51pm

Edited: 29/08/2012 10:51pm

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??