如何解决SQLSTATE[42S22]:未找到列:1054 yii2 中“where 子句”中的未知列“guidian.id”
我是 Yii2 PHP 框架的新手,我有学生表格来获取学生详细信息和 guardian id_no
,提交时我收到此错误 sqlSTATE[42S22]: Column not found: 1054 UnkNown column 'guidian.id' in 'where clause' The sql being executed was: SELECT EXISTS(SELECT * FROM guidian WHERE guidian.id='123')
学生表有 guidian_id
作为 {{ 1}} 在 foreign key
表上引用 id_no
,我对说 guidian
的错误感到困惑,而实际上我没有在 guidian.id
中指定 id
作为列表(我在 guidian 表中没有 guidian
列)。注意:我理解错别字 id
而不是 guidian
这是guardian
ManageStudentController.PHP
这里是模型 <?PHP
namespace app\controllers;
use app\models\district;
use app\models\Subclass;
use app\models\Studentclass;
use app\models\Guidian;
use Yii;
use app\models\Student;
use app\models\StudentSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\Session;
/**
* ManageStudentController implements the CRUD actions for Student model.
*/
class ManageStudentController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,'only' => ['logout','index','view','create','update','delete','district-list'],'rules' => [
[
'actions' => ['logout','allow' => true,'roles' => ['@'],],'verbs' => [
'class' => VerbFilter::class,'actions' => [
'delete' => ['GET','POST'],];
}
/**
* Lists all Student models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new StudentSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index',[
'searchModel' => $searchModel,'dataProvider' => $dataProvider,]);
}
/**
* displays a single Student model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view',[
'model' => $this->findModel($id),]);
}
/**
* Creates a new Student model.
* If creation is successful,the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Student();
// die(var_dump($model->first_name));
// die(var_dump(Yii::$app->request->post()));
if ($model->load(Yii::$app->request->post())) {
$Studentclass = new Studentclass();
$Studentclass->class_id = $model->class_id;
$Studentclass->subClass_id = $model->subClass_id;
$Studentclass->student_id = $model->adm_no;
//die(var_dump($model->subClass_id));
if(!$model->control_field){//this guidian is not existing in db
//var_dump($model);exit();
$modelGuidian = new Guidian();
$modelGuidian->id_no = $model->guidian_id;
$modelGuidian->id_type = $model->id_type;
if($modelGuidian->save()){
$model->save();
$Studentclass->save();
return $this->redirect(array('guidian/create?guidian_id='.$modelGuidian->id_no));
}
}else{
$model->save();
$Studentclass->save();
}
}
return $this->render('create',[
'model' => $model,]);
}
public function actionCheckGuidian(){
$model = new Guidian();
$searchParams = Yii::$app->request->get();
if($searchParams["id_type"] && $searchParams["guidian_id"]) {
$model = Guidian::find()->where(['id_type'=>$searchParams["id_type"],'id_no'=>$searchParams["guidian_id"]])->one()??$model;
$flag = $model ->isNewRecord?'0':'1';
}
echo $flag;
}
/**Getting the list of districts based to the selected region
* passed region Id as parameter
*/
public function actiondistrictList($district){
$districtList = district::findBysql('select * from district where region_id ="'.$district.'"')->all();
//return $districtList;
if(count($districtList) > 0){
foreach($districtList as $value){
echo "<option value = '".$value->id."'>".$value->name."</option>";
}
}else{
echo "<option>...</option>";
}
}
/**Getting the list of sub-classes based to the selected class
* passed class Id as parameter
*/
public function actionClassList($class_id){
$subclassList = Subclass::findBysql('select * from subclass where class_id ="'.$class_id.'"')->all();
//return $districtList;
if(count($subclassList) > 0){
foreach($subclassList as $value){
echo "<option value = '".$value->id."'>".$value->name."</option>";
}
}else{
echo "<option>...</option>";
}
}
/**
* Updates an existing Student model.
* If update is successful,the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('flash','Remove');
return $this->redirect(['view','id' => $model->adm_no]);
}
return $this->render('update',]);
}
/**
* Deletes an existing Student model.
* If deletion is successful,the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
Yii::$app->session->setFlash('flash','Remove');
return $this->redirect(['index']);
}
/**
* Finds the Student model based on its primary key value.
* If the model is not found,a 404 HTTP exception will be thrown.
* @param integer $id
* @return Student the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Student::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Student.PHP
这里是监护人模型 <?PHP
namespace app\models;
use Yii;
/**
* This is the model class for table "student".
*
* @property int $adm_no
* @property string|null $first_name
* @property string|null $middle_name
* @property string|null $last_name
* @property string|null $gender
* @property string|null $date_of_birth
* @property int|null $guidian_id
* @property string|null $guidian_relation
* @property int|null $home_region_id
* @property int|null $home_district_id
* @property int|null $status
*
* @property Payment[] $payments
* @property district $homedistrict
* @property Guidian $guidian
* @property Region $homeRegion
* @property Studentclass[] $studentclasses
*/
class Student extends \yii\db\ActiveRecord
{
public $id_type;
public $class_id;
public $subClass_id;
public $control_field;
// public $id_no;
public $date_joined;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'student';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['date_of_birth','guidian_id','control_field','date_joined'],'safe'],[['home_region_id','home_district_id','status','class_id','subClass_id'],'integer'],[['first_name','middle_name','last_name'],'string','max' => 100],[['gender'],'max' => 6],[['guidian_relation'],'max' => 255],[['home_district_id'],'exist','skipOnError' => true,'targetClass' => district::className(),'targetAttribute' => ['home_district_id' => 'id']],[['guidian_id'],'targetClass' => Guidian::className(),'targetAttribute' => ['guidian_id' => 'id']],[['home_region_id'],'targetClass' => Region::className(),'targetAttribute' => ['home_region_id' => 'id']],];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'adm_no' => yii::t('app','Admission No:'),'first_name' => yii::t('app','First Name:'),'middle_name' => yii::t('app','Middle Name:'),'last_name' => yii::t('app','Last Name:'),'gender' => yii::t('app','Gender:'),'date_of_birth' => yii::t('app',''),//label text is on view page
'guidian_id' => yii::t('app','Guidian Id number:'),'guidian_relation' => yii::t('app','Guidian Relation:'),'home_region_id' => yii::t('app','Home Region:'),'home_district_id' => yii::t('app','Home district:'),'status' => yii::t('app','Status:'),'id_type' => yii::t('app','Guidian ID Type:'),'class_id' => yii::t('app','Class:'),'subClass_id' => yii::t('app','Sub Class:'),'date_joined' => yii::t('app',];
}
/**
* Gets query for [[Payments]].
*
* @return \yii\db\ActiveQuery
*/
public function getPayments()
{
return $this->hasMany(Payment::className(),['student_id' => 'adm_no']);
}
/**
* Gets query for [[Homedistrict]].
*
* @return \yii\db\ActiveQuery
*/
public function getHomedistrict()
{
return $this->hasOne(district::className(),['id' => 'home_district_id']);
}
/**
* Gets query for [[Guidian]].
*
* @return \yii\db\ActiveQuery
*/
public function getGuidian()
{
return $this->hasOne(Guidian::className(),['id_no' => 'guidian_id']);
}
/**
* Gets query for [[HomeRegion]].
*
* @return \yii\db\ActiveQuery
*/
public function getHomeRegion()
{
return $this->hasOne(Region::className(),['id' => 'home_region_id']);
}
/**
* Gets query for [[Studentclasses]].
*
* @return \yii\db\ActiveQuery
*/
public function getStudentclasses()
{
return $this->hasMany(Studentclass::className(),['student_id' => 'adm_no']);
}
}
guidian.PHP
解决方法
抱歉,我已经解决了,规则函数中有一个 Id 关联了两个表
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。