从条件CakePHP的数据库中选择数据来自2表

时间:2017-03-18 02:42:03

标签: php mysql cakephp

我有两个表StandardsTopicsStandard包含id, user_id, nameTopics包含id, user_id, name, school_id

我想使用用户登录条件从StandardsTopics检索数据。所以用户只能看到他们的学校名称。

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

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

这是我的控制者:

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
            }
        }
    }
    ?>

2 个答案:

答案 0 :(得分:0)

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

  

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

$topics = $this->Topic->find('all', [
    'conditions' => [
        'user_id' => $this->Auth->user('id')
    ]
]);
$standards = $this->Standard->find('all', [
    'conditions' => [
        'user_id' => $this->Auth->user('id')
    ]
]);

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

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

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

答案 1 :(得分:0)

您正在使用主题ID检查登录用户的ID。因此,在您的情况下,您需要使用主题user_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));      
}