简体   繁体   中英

Best way to query in CodeIgniter

I tried to look for an answer of this but I have a question about filtering with codeigniter. The code below checks if the administrator is logged in shows every idea(brain) status in the query, which could be public or hidden, otherwise, it only shows the ones with public status.

I want to change this to keep showing the ideas with public status to everyone and only the ideas with hidden status of the person that is logged in.

if($this->ion_auth->is_admin()) {
    $brain_status = null;
}
else {
    $brain_status = $brain::PUBLIC_STATUS;
}

$brain_collection = $this->em->getRepository('Entities\Brain')
                             ->getListPerPage($brain::BRAIN_PER_PAGE, $page, $brain_status);

I tried using $this->session->user_id and I don't know how many other syntaxes and I can't get it to work, every idea has a field in the database where the user_id is logged when the idea is created to accomplish this.

Any help is extremely appreciated

to complete this Q/A pair, I'm transforming my comments into an answer:

the requirement:

admin sees all ideas: public and hidden, regular user only sees public ideas

the solution:

create a database column "published", associated to each idea (and consequently user_id). Now you can show in your view all records which match published=1 to everyone and those published=0 only to whom where user_id matches (or admins).

If a user is logged-in (you set a session with their user_id) and if this user_id matches the user_id of the idea then you show it. It has the advantage that you could implement for a user to be able to publish or un-publish their own idea , without admin intervention, only viewable to themselves, unless published by admin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM