DataMapper ORM - selecting where there is no relationship |
[eluser]NeilyM[/eluser]
Hi Guys I have been staring at this for a few hours today and I'm hoping that I'm simply not seeing the wood for the trees now. I have contacts and emails. A contact "has_many" emails. An email "has_many" contacts. I can easily select contacts that have been sent a particular email (eg the one with id 5) like this: $contact = new Contact; $contact->where_related("email", "id", 5); $contact->get(); But what I actually need is a way to select contacts who have not received the email with id 5. I guess it would be something like: $contact = new Contact; $contact->where_not_related("email", "id", 5); $contact->get(); ...but of course there is no "where_not_related" method. This sounds so simple but I'm really tearing my hair out. Can anyone help me out? Cheers, Neil
[eluser]WanWizard[/eluser]
You can simply include the operator in the field definition: Code: $contact = new Contact;
[eluser]NeilyM[/eluser]
Thanks WanWizard. I tried that but unfortunately it doesn't return the contacts who have not received email with id 5 What it does do is return contacts who have received emails with other ids. However, I need to be able to select all contacts who have not received email with id=5 so that I can send that email to them. Again, thanks. Neil
[eluser]WanWizard[/eluser]
True. Let's try something else then Code: // create the objects You can probably create something similar using where_not_in() combined with a subquery.
[eluser]NeilyM[/eluser]
Thanks again WanWizrd. I also thought of something else based on the success of that was posted here: http://ellislab.com/forums/viewthread/170368/ ...and I'm wondering if this will work: $contact = new Contact; $contact->where_related(“email”, “id !=”, 5); $contact->or_where_related(“email”, “id”, NULL); $contact->get(); I'll try these when I get into work tomorrow. Thanks again. Cheers, Neil
[eluser]WanWizard[/eluser]
That's not going to work, but this should: Code: // create the objects This generates Code: SELECT *
[eluser]NeilyM[/eluser]
Thanks WanWizard. I'll get on the case tomorrow and post back. Thank you for your time.
[eluser]NeilyM[/eluser]
WanWizard, your solution works a treat. Thank you :-) Cheers, Neil |
Welcome Guest, Not a member yet? Register Sign In |