TableServer dynamic query based on login user
TableServer dynamic query based on login user
On the tableserver.php, currently, I have it where the query will search on the $_Session['currentUser'] and this works fine. However, if I have a specific user (for instance 'Admin'), I've tried disabling that where clause so that it would show all the data but I get an error. Is there a way to do if and then for the queries based on a user? My original code below:
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'table', 'id' )
->fields(
Field::inst('AssignedTo'),
Field::inst('Note'),
Field::inst('Completed')
)
->where( 'Completed', NULL )
if($_SESSION['currentUser'] != 'Admin') {
->where( 'AssignedTo', $_SESSION['currentUser'])
}
This question has accepted answers - jump to:
Answers
Im thinking you are looking for an
->and_where
instead of just a->where
.The way I read your code the SQL would look something like the following (if the user was NOT an admin):
I am not a PHP expert, but I am thinking if you used the
->and_where
instead it would look something like the following (if the user was NOT an admin):Your PHP syntax is invalid. The idea is right though. Try:
Or:
The first one works by using an anonymous function and adding an extra condition if needed (since its a function we can run logic checks).
The second one works by simply breaking the chain rather than using one long chained method set.
Regards,
Allan