Вот код контроллера:
Код: Выделить всё
<?php
namespace app\controllers;
use app\models\Student;
use yii\rest\ActiveController;
class StudentController extends ActiveController
{
public $modelClass = 'app\models\Student';
public function actions()
{
$actions = parent::actions();
unset($actions['create']);
return $actions;
}
public function actionCreate()
{
$model = new Student();
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
$model->username = $model->newStudent();
return $model;
}
}
Код: Выделить всё
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "Student".
*
* @property integer $id
* @property string $firstname
* @property string $lastname
* @property string $middlename
* @property string $birthdate
* @property string $address
* @property string $iin
* @property string $cardnumber
* @property string $phonenumber
* @property string $username
* @property string $password
* @property integer $group_id
*
* @property Group $group
*/
class Student extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Student';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['firstname', 'lastname', 'middlename', 'birthdate', 'address', 'iin', 'cardnumber', 'phonenumber', 'username', 'password', 'group_id'], 'required'],
[['birthdate'], 'safe'],
[['group_id'], 'integer'],
[['firstname', 'lastname', 'middlename'], 'string', 'max' => 100],
[['address'], 'string', 'max' => 255],
[['iin', 'phonenumber'], 'string', 'max' => 20],
[['cardnumber'], 'string', 'max' => 30],
[['username'], 'string', 'max' => 50],
[['password'], 'string', 'max' => 60],
[['iin'], 'unique'],
[['cardnumber'], 'unique'],
[['phonenumber'], 'unique'],
[['username'], 'unique'],
[['group_id'], 'exist', 'skipOnError' => true, 'targetClass' => Group::className(), 'targetAttribute' => ['group_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'firstname' => 'Firstname',
'lastname' => 'Lastname',
'middlename' => 'Middlename',
'birthdate' => 'Birthdate',
'address' => 'Address',
'iin' => 'Iin',
'cardnumber' => 'Cardnumber',
'phonenumber' => 'Phonenumber',
'username' => 'Username',
'password' => 'Password',
'group_id' => 'Group ID',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getGroup()
{
return $this->hasOne(Group::className(), ['id' => 'group_id']);
}
public function newStudent()
{
$lastUsername = Yii::$app->db->createCommand('SELECT `username`
FROM `Student` ORDER BY id DESC LIMIT 1');
if ($lastusername != null) {
$numberOfUsername = explode("ent", $lastusername);
return 'student' . $numberOfUsername[1] + 1;
}
return 'student1';
}
}
Код: Выделить всё
{
"name": "PHP Fatal Error",
"message": "Class 'app\\controllers\\Yii' not found",
"code": 1,
"type": "yii\\base\\ErrorException",
"file": "C:\\OpenServer\\domains\\univer.site\\controllers\\StudentController.php",
"line": 23,
"stack-trace": [
"#0 [internal function]: yii\\base\\ErrorHandler->handleFatalError()",
"#1 {main}"
]
}