gpt4 book ai didi

php - 根据条件从数据库中的 2 个表中选择数据 CakePHP

转载 作者:行者123 更新时间:2023-11-29 19:13:54 25 4
gpt4 key购买 nike

我有两个表StandardsTopicsStandard包含id、user_id、name主题包含id、user_id、name、school_id

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

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

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

这是我的 Controller :

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));
}

这是我的 View 文件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
}
}
}
?>

最佳答案

您正在使用主题 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));
}

关于php - 根据条件从数据库中的 2 个表中选择数据 CakePHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com