使用条件CakePHP从数据库中的2个表中选择数据

使用条件CakePHP从数据库中的2个表中选择数据,php,mysql,cakephp,Php,Mysql,Cakephp,我有两个表标准和主题,标准包含id、用户id、姓名和主题包含id、用户id、姓名、学校id public function admin_subject_list($standard_id = null) { $this->loadModel('Subject'); $this->Subject->bindModel(array( 'belongsTo'=>array(

我有两个表
标准
主题
标准
包含
id、用户id、姓名
主题
包含
id、用户id、姓名、学校id

public function admin_subject_list($standard_id = null)
{
    $this->loadModel('Subject');
    $this->Subject->bindModel(array(
                    'belongsTo'=>array(
                        'Standard'
                    ),
                    'hasMany'=>array(
                        'Topic'
                    )
    ),false);

    $standard_data = $this->Standard->find('first',array('conditions'=>array('Standard.id'=>$standard_id)));

    $this->Subject->Behaviors->load('Containable');
    $userId = $this->Auth->user('id');
    $filters = array('Subject.standard_id'=>$standard_id);
    $data = $this->Subject->find('all',array('conditions'=>$filters, 'contain' => array('Standard', 'Topic' => array('conditions' => array('Topic.user_id' => $userId))))); 

    $this->set(compact('data','standard_id','standard_data'));
    $this->set('title_for_layout', __('Standards', true));      
}
我想根据用户登录条件从
标准
主题
检索数据。因此,用户只能看到他们的学校名称

错误:错误是,它将显示学校名称属于所有用户,而不仅仅是登录用户

我想显示登录的学校名称用户

这是我的控制器:

public function admin_subject_list($standard_id = null)
{
    $this->loadModel('Subject');
    $this->Subject->bindModel(array(
                        'belongsTo'=>array(
                            'Standard'
                        ),
                        'hasMany'=>array(
                            'Topic'
                        )
    ),false);

    $standard_data = $this->Standard->find('first',array('conditions'=>array('Standard.id'=>$standard_id)));

    $filters        =   array('Topic.id'=>$this->Auth->user('id'));

    $filter = array('Subject.standard_id'=>$standard_id);
    $data = $this->Subject->find('all',array('conditions'=>$filter));       
    $this->set(compact('data','standard_id','standard_data'));
    $this->set('title_for_layout', __('Standards', true));      
}   
这是我的视图文件standartd_list.ctp

<?php
    }
    // pr($data);
    $color = array('bg-kypta','bg-green','bg-aqua','bg-uni-n','bg-light-blue','bg-orange','bg-ferozi','bg-light-bluen','bg-light-yellow','bg-teal','bg-maroon');
    foreach($data as $key=>$value)
    {
        $random = rand(0,10);
        $get_subscribe_subject = $this->General->getSubscribeTutor($value['Subject']['id']);
        if(empty($get_subscribe_subject))
        {
        ?>          
        <div class="col-lg-3 col-xs-6">
            <!-- small box -->
            <div class="small-box <?php echo $color[$random]?>">
                <div class="icon">
                    <i class="ion ion-person-add"></i>
                </div>
                <a style="width:33.33%;float:left;"  class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'view',$value['Subject']['id']))?>">
                    <i class="fa fa-arrow-circle-right"></i>&nbsp; Preview 
                </a>
               <a style="width:33.33%;float:left;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'edit',$value['Subject']['id']))?>"><i class="fa fa-cog" aria-hidden="true"></i>&nbsp; Setting
                </a>
                <a style="width:33.33%;float:left;" data-tab="yes" data-rel="<?php echo $value['Subject']['id'];?>" class="small-box-footer delete_subject_class" href="javascript:void(0)"><i class="fa fa-trash-o" aria-hidden="true"></i>&nbsp; Delete
                </a>
            </div>
        </div>
        <?php 
        }
        else
        {
            // pr($get_subscribe_subject);die;
            foreach($get_subscribe_subject as $k=>$v)
            {
                $random = rand(0,10);
            ?>
            <div class="col-lg-3 col-xs-6">
            <!-- small box -->
                <div class="small-box <?php echo $color[$random]?>">
                    <div class="inner">
                        <h3>
                            <?php
                            if($v['SubjectTutor']['status'] == 1)
                            {
                                $is_approved = 'yes';
                            }
                            else
                            {
                                $is_approved = 'no';
                            }
                            //goToTopicList
                            ?>
                            <a data-subject_tutor_id="<?php echo $v['SubjectTutor']['id'];?>" data-is_approved="<?php echo $is_approved;?>" style="color:#fff;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'topic_list',$v['SubjectTutor']['id']))?>">
                            <?php echo $value['Subject']['name']?> <?php echo $v['School']['name']?> <br>by <?php echo $v['Tutor']['first_name']." ".$v['Tutor']['last_name'] ?>
                            </a>
                            <br>
                        </h3>
                        <p>
                        <?php
                        $get_subject_topic_count = $this->General->getSubjectTutorTopicCount($v['Tutor']['id'],$value['Subject']['id'],$v['SubjectTutor']['province_id'],$v['SubjectTutor']['city_id'],$v['SubjectTutor']['school_id'],$v['SubjectTutor']['state_private']);
                        echo count($get_subject_topic_count);
                        ?> 
                        Topic(s)</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-person-add"></i>
                    </div>
                    <a style="width:33.33%;float:left;"  class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'view_tutor_subject',$v['SubjectTutor']['id']))?>">
                        <i class="fa fa-arrow-circle-right"></i>&nbsp; Preview 
                    </a>
                    <!--
                    <a style="width:33.33%;float:left;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'edit',$value['Subject']['id']))?>"><i class="fa fa-cog" aria-hidden="true"></i>&nbsp; Setting
                    </a>-->
                    <?php
                    $check_student_subscribe = $this->General->getSubscribeInfo($v['Tutor']['id'],$value['Subject']['id']);
                    if(empty($check_student_subscribe))
                    {
                        $is_deletable = 'yes';
                    }
                    else
                    {
                        $is_deletable = 'no';
                    }
                    ?>
                    <a style="width:33.33%;float:left;" data-tab="<?php echo $is_deletable?>", data-subject_tutor_id="<?php echo $v['SubjectTutor']['id'];?>" class="small-box-footer delete_item_class" href="javascript:void(0)"><i class="fa fa-trash-o" aria-hidden="true"></i>&nbsp; Delete</a>
                </div>
            </div>
        <?php
            }
        }
    }
    ?>


看起来您正在使用CakePHP 2.x,对吗?还有一点不清楚您在问什么,但我相信您应该能够使用标准的
find()
s

我想显示登录的学校名称用户

这将查找与登录用户关联的所有主题,以及与登录用户关联的所有标准

或者,您甚至可以使用Cake中提供的动态查找器来简化它:

$topics = $this->Topic->findByUserId($this->Auth->user('id'));
$standards = $this->Standard->findByUserId($this->Auth->user('id'));

您正在使用主题id检查登录用户id。因此,在您的情况下,您需要使用主题的
用户id检查登录用户id

public function admin_subject_list($standard_id = null)
{
    $this->loadModel('Subject');
    $this->Subject->bindModel(array(
                    'belongsTo'=>array(
                        'Standard'
                    ),
                    'hasMany'=>array(
                        'Topic'
                    )
    ),false);

    $standard_data = $this->Standard->find('first',array('conditions'=>array('Standard.id'=>$standard_id)));

    $this->Subject->Behaviors->load('Containable');
    $userId = $this->Auth->user('id');
    $filters = array('Subject.standard_id'=>$standard_id);
    $data = $this->Subject->find('all',array('conditions'=>$filters, 'contain' => array('Standard', 'Topic' => array('conditions' => array('Topic.user_id' => $userId))))); 

    $this->set(compact('data','standard_id','standard_data'));
    $this->set('title_for_layout', __('Standards', true));      
}

是的,我正在使用CakePHP2.x。我改变了$data=$this->Subject->findByUserId($this->Auth->user('id');但它什么都没有。请告诉我应该删除或更改哪一行可以显示
$data
的调试吗?当我编译程序时,将显示所有用户的学校名称,而不仅仅是用户日志on@Rik.esh你好我忘了我在generalhelper.php中还有一个函数请查看并帮助我错误:未找到列:1054未知列“where子句”中的“Topic.user_id”,但我确信Topics表包含user_id,我已使用containable with conditions更新了答案。请检查。它仍然显示所有用户学校名称,而不仅仅是用户登录。对不起,兄弟,你的问题让我越来越困惑。