我正在使用带有xampp的yii 2.0并尝试运行此查询
$model = Students::find()->where('student_id = '.$id.'')->one();
和student_id
是主键,正如预期的那样,此查询应该只返回一个返回的数据,但返回的数据相同。
查询的输出如下
`object(app\models\Students)#72 (8) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(12) {
["student_id"]=>
int(1)
["student_name"]=>
string(10) "Anil Kumar"
["student_parent_id"]=>
int(1)
["student_class"]=>
string(1) "1"
["student_section"]=>
string(1) "A"
["st_admsn_date"]=>
string(10) "2017-07-01"
["student_image_url"]=>
string(0) ""
["st_perm_address"]=>
string(29) "31 APC Road, Kolkata - 700023"
["st_present_address"]=>
string(29) "31 APC Road, Kolkata - 700023"
["st_blood_group"]=>
string(2) "B+"
["st_medical_history"]=>
string(2) "OK"
["school_id"]=>
int(1)
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(12) {
["student_id"]=>
int(1)
["student_name"]=>
string(10) "Anil Kumar"
["student_parent_id"]=>
int(1)
["student_class"]=>
string(1) "1"
["student_section"]=>
string(1) "A"
["st_admsn_date"]=>
string(10) "2017-07-01"
["student_image_url"]=>
string(0) ""
["st_perm_address"]=>
string(29) "31 APC Road, Kolkata - 700023"
["st_present_address"]=>
string(29) "31 APC Road, Kolkata - 700023"
["st_blood_group"]=>
string(2) "B+"
["st_medical_history"]=>
string(2) "OK"
["school_id"]=>
int(1)
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}`
这是我的模特
<?php
namespace app\models;
use Yii;
class Students extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'students';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['student_name', 'student_parent_id', 'student_class', 'student_section', 'st_admsn_date', 'student_image_url', 'st_perm_address', 'st_present_address', 'st_blood_group', 'st_medical_history'], 'required'],
[['student_parent_id', 'school_id'], 'integer'],
[['st_admsn_date'], 'safe'],
[['student_name'], 'string', 'max' => 200],
[['student_class'], 'string', 'max' => 100],
[['student_section', 'st_blood_group'], 'string', 'max' => 50],
[['student_image_url'], 'string', 'max' => 400],
[['st_perm_address', 'st_present_address', 'st_medical_history'], 'string', 'max' => 300],
];
}
public function attributeLabels()
{
return [
'student_id' => 'Student ID',
'student_name' => 'Student Name',
'student_parent_id' => 'Student Parent ID',
'student_class' => 'Student Class',
'student_section' => 'Student Section',
'st_admsn_date' => 'St Admsn Date',
'student_image_url' => 'Student Image Url',
'st_perm_address' => 'St Perm Address',
'st_present_address' => 'St Present Address',
'st_blood_group' => 'St Blood Group',
'st_medical_history' => 'St Medical History',
'school_id' => 'School ID',
];
}
}
我想从结果中删除所有_oldAttributes
数据。
感谢任何帮助。谢谢。
答案 0 :(得分:0)
我在$model
$result = $model->getAttributes();
它为我提供了数组中的数据。 如果你想使用这个对象,你不应该为oldAttributes烦恼。 在涉及单个对象的操作中,它不会介入。
答案 1 :(得分:0)
首先需要改进查询:
$model=Student::findOne([$id]);
echo "<pre>";print_r($model);echo "</pre>";die;
您可以直接将主键传递给findOne函数。