我有兩個表Standards
和Topics
,Standard
包含id, user_id, name
和Topics
包含id, user_id, name, school_id
。選擇數據從數據庫中的表2的條件CakePHP的
我想從用戶登錄條件中檢索Standards
和Topics
中的數據。所以用戶只能看到他們的學校名稱。
錯誤:錯誤是,它將顯示學校名稱屬於所有用戶,而不僅僅是登錄用戶。
我想顯示登錄的學校名稱用戶。
這是我的控制器:
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> 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> 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> 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> 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> 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> Delete</a>
</div>
</div>
<?php
}
}
}
?>
請問您能顯示'$ data'的調試嗎? – Aarrbee
當我編譯程序的所有用戶的學校名稱將被顯示,而不僅僅是用戶登錄 – wendy
@ Rik.esh嗨。我忘了我在generalhelper.php中有一個更多的功能,請參閱並幫助我https://stackoverflow.com/questions/42895678/select-data-from-2-table-with-condition-cakephp-2-x – wendy