Search for beans that lack a given tag in a RedBean-ish way

48 views
Skip to first unread message

Jonathan Bromley

unread,
Apr 29, 2021, 1:59:21 PM4/29/21
to redbe...@googlegroups.com
Hello,

First, thanks to the author(s) for the very nice ORM which has made my life a lot easier.

However.... I suspect I'm not using it fully, often writing SQL queries to find things that can probably be done more neatly within RedBean.

Specifically: how do I find beans that do *not* have a given tag? Suppose I have tables "users", "tag" and "tag_users". I want to find all rows (beans) in "users" that do NOT have tag T. At present I find T in the tag table so I know its ID - let's call it T_id - and then I do this SQL:

SELECT * FROM
users u LEFT JOIN tag_users tu ON u.id = tu.users_id AND tu.tag_id=T_id
WHERE tu.id IS NULL

This works fine, but it doesn't fit nicely with other RedBean code. Any better way?

Jonathan

gabor

unread,
May 3, 2021, 3:32:25 PM5/3/21
to redbeanphp
Hi Jonathan,

Unfortunately there is no pre-programmed way to fetch all untagged beans, that being said, using good old SQL is pretty much the 'RedBeanish' way, after all RedBeanPHP does not have a query builder and has been designed to interface nicely with SQL. You could use https://redbeanphp.com/index.php?p=/querying#find_from_sql findFromSQL to obtain those records as beans for instance. Hope this helps.

cheers,
Gabor

Jonathan Bromley

unread,
May 4, 2021, 10:09:44 AM5/4/21
to redbeanphp
OK, that makes sense. I have to admit I had overlooked findFromSQL, and it looks really useful. Thanks.

There's also a case to be made that if I routinely need to find the "non-tag" cases, then I shouldn't be using tags at all but some other relationship (maybe enums?) instead. Thought for another day.

Thanks again
Jonathan

Reply all
Reply to author
Forward
0 new messages