CodeIgniter Forums
Add "global" where clause to any SQL-Query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Add "global" where clause to any SQL-Query (/showthread.php?tid=32913)



Add "global" where clause to any SQL-Query - El Forum - 08-09-2010

[eluser]Unknown[/eluser]
Hi there!
Does anyone know if it's possible to add a Where-Clause which will be added to any SQL-Query automatically?

For example: I want to make sure my users select/update/delete only their DB-data. So for Security I added to all my SQL-Querys:

$this->db->where('owner', $user_id);


For simplicity and I don't forget to add this where-clause somewhere, is it possible to setup somehow a "globally" valid where-statement, which is added to any sql-query by default? This doesn't need to be application-wide, but at least Model-wide

Thanks for any hints!


Add "global" where clause to any SQL-Query - El Forum - 08-09-2010

[eluser]mddd[/eluser]
Check out Active Record caching.
It allows you to store pieces of queries just like your 'where owner=$user_id' piece.


Add "global" where clause to any SQL-Query - El Forum - 08-09-2010

[eluser]Unknown[/eluser]
great thanks a lot!

actually this does exactly what I asked for ... but after testing it, I see that I can't actually work this way ;-) as this where-clause is now added to ALL queries (yeah, i know I asked for it Wink - so also to queries which don't have an "owner"-column like the CI-"session"-table, or e.g. I'm using ION_AUTH which uses Joins so a simple "where(‘owner’, $user_id)" will end up in an db-error: "Column 'owner' in where clause is ambiguous" (because of using joins).

any other idea I can add this now not "globally" but only Model-wide, so it is only effect my Model-Queries?