I also have a table called ‘Student’ that has a 1=1 relationship with person.
When you enter a Person record, and the Person.Type is set to ‘Student’, I want a new record to get created in the Student table, with the same ID as the Person record, ready for use later.
This is what I tried in the Persons model, but it does not create the record:
public function afterSave($insert, $changedAttributes){
if($insert && $this->type == 'Student'){
$student_create = new Student();
$student_create->id = $this->id;
$student_create->save();
}
}
I can see in the article above how to retrieve relational data, but I could not see about creating it.
So I am hoping I am going down the right road here?
public function actionCreate()
{
$model = new Persons;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// you saved the $model so now you have $model->IdPerson or whatever is your id key
if($model->Type=='Student') {
$student= new Students;
// here you set all students model value
$student->save();
}
.......
For some reason the debug bar at the bottom of the screen is not showing and my debug data ends a few days ago.
This is despite the fact I have the folling in my index.php file:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
And in my modules I have added the following line:
'debug' => 'yii\debug\Module',
I am using an alias on my apache development server, so is it possble that somehow convinces the yii app that we are not running on localhost, and so hides the debug?
About the debug problem we will see but in the meantime you can do it in the old dirty way:
if ($model->load(Yii::$app->request->post()) && $model->save()) {
echo "<pre>";
print_r($app->request->post()); //this print the post content
print_r($model); // this print the model just after you saved it
echo "type: '".$model->type."'=='Student'\n";
echo "</pre>";
die(); // break execution before you enter the view,otherwise the buffer will be deleted and you don't get the output
if($model->type=='Student') {
$student = new Student;
$student->id = $model->id;
$student->save();
}
Just so you know, I had to start over, and now I have persons and students as my tables
here is what I am trying now when creating the Persons record, I’m just trying to create the students record for now regardless of what type is set to, just to see what happens:
public function actionCreate()
{
$model = new Persons();
$student = new Students();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$student->personsPersonID = $model->personID;
$student->setIsNewRecord(true);
$student->save();
return $this->redirect(['view', 'id' => $model->personID]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
According to the debugger: "Model not inserted due to validation error"
Then AFTER that in the log it seems to do the insert into the Persons table.
So I think what’s happened is that the student record is trying to insert BEFORE the persons, and failing due to my foreign key constraints on the database.
Is there a way to perform the students insert AFTER the persons has completed?