PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] Error: getting unknown property: yii\web\Application::generateid

 March 04, 2022     yii, yii2, yii2-basic-app     No comments   

Issue

I have the below code in studentsController.php to add data to 2 tables from a form but im getting error. It is to add to students table, then add some data to the users table too. I feel i'm supposed to add something at the top via "use' keyword but I don't know.please help.

public function actionCreate() {
    $model = new model();
    $user = new Users();

    if ($model->load(Yii::$app->request->post())) {

        $model->id = Yii::$app->generateid->getGUID();  //this line caused the error
        $model->save();
        $studentId = $model->id;
        if($user->load(Yii::$app->request->post()))
        {
            $user->id = $model->id;
            $user->firstname = $model->firstname;
            $user->lastname = $model->lastname;
            $user->username = $model->phone;
            $user->password = 'pass123';   //default password
            $user->role = 'student';
            $user->save(false);
        }
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
         return $this->render('create', [
             'model' => $model,
             'user' => $user,                 
         ]);
    }
}

Solution

You seem to miss generateid in your web.php It should be something like this

GenerateIDs.php inside @app/components folder

<?php 
namespace app\components; 

class GenerateIDs extends \yii\base\Component
{
    public function getGUID()
    {
       //do your stuff here
    }
    //....
}

then in your @app/config/web.php you have something like

<?php
return [
    'components' => [
        'generateid' => [
            'class'=>'app\components\GenerateIDs',
            // other configurations for the component
        ],
    ],
];

then you can use it in your app as you wish

public function actionCreate() {
$model = new model();
$user = new Users();

if ($model->load(Yii::$app->request->post())) {

    $model->id = Yii::$app->generateid->getGUID();  //this line caused the error
    $model->save();
    $studentId = $model->id;
    if($user->load(Yii::$app->request->post()))
    {
        $user->id = $model->id;
        $user->firstname = $model->firstname;
        $user->lastname = $model->lastname;
        $user->username = $model->phone;
        $user->password = 'pass123';   //default password
        $user->role = 'student';
        $user->save(false);
    }
    return $this->redirect(['view', 'id' => $model->id]);
} else {
    return $this->render('create', [
                'model' => $model,
                'user' => $user,                 
    ]);
}

Please take a look at Yii2 guide on Components



Answered By - Stefano Mtangoo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing