简体   繁体   English

在 CodeIgniter 中查询的最佳方式

[英]Best way to query in CodeIgniter

I tried to look for an answer of this but I have a question about filtering with codeigniter.我试图寻找这个问题的答案,但我有一个关于使用 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.我尝试使用$this->session->user_id并且我不知道有多少其他语法而且我无法让它工作,每个想法在数据库中都有一个字段,当创建想法时记录 user_id 到做到这一点。

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).创建一个数据库列“已发布”,与每个想法(以及因此 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).现在,您可以在您的视图中显示所有与published=1匹配的记录和那些仅与user_id 匹配的人(或管理员)的published=0记录。

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.如果用户已登录(您使用他们的 user_id 设置会话)并且此 user_id 与该想法的user_id 匹配,则您将其显示出来。 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.它的优点是您可以为用户实现发布或取消发布他们自己的想法,无需管理员干预,除非由管理员发布,否则仅对他们自己可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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