2017-03-18 63 views
0

我有两个表StandardsTopicsStandard包含id, user_id, nameTopics包含id, user_id, name, school_id选择数据从数据库中的表2的条件CakePHP的

我想从用户登录条件中检索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 
      } 
     } 
    } 
    ?> 
+0

请问您能显示'$ data'的调试吗? – Aarrbee

+0

当我编译程序的所有用户的学校名称将被显示,而不仅仅是用户登录 – wendy

+0

@ Rik.esh嗨。我忘了我在generalhelper.php中有一个更多的功能,请参阅并帮助我https://stackoverflow.com/questions/42895678/select-data-from-2-table-with-condition-cakephp-2-x – wendy

回答

0

你被检查登录的用户与题目的ID。所以,在你的情况下,你需要检查登录用户的ID与主题的user_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));  
} 
+0

错误:找不到列:1054'Where子句'中的未知列'Topic.user_id' – wendy

+0

但iam确定主题表containe user_id – wendy

+0

我已通过使用包含条件更新答案。请检查。 – Aarrbee

0

它看起来像你使用CakePHP 2.x的,对不对?这也有点不清楚你在问什么,但我相信你应该只能使用标准find() s。

I want to display school name user who log on.

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

这会发现,关联w的用户登录/所有主题,以及与登录的用户相关联的所有标准。

或者,你甚至可以用在蛋糕中可用的动态查找器简化它:

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

yes im使用cakephp 2.x.我已经改变$ data = $ this-> Subject-> findByUserId($ this-> Auth-> user('id')); \t但它没有任何损失。请告诉我什么行我应该删除或更改 – wendy