lalithkumar
lalithkumar

Reputation: 3540

Yii Framework 2.0 -findModel returns an empty object

I am trying to show the form values which I have stored in the database.But it's not happening and also returning the empty values.I have added whatever I did.

Controller

protected function findModel($id)
    {
        if (($model = UserRegistration::find($id)->one()) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

Model

this is my model.I have declared all variables as public.Is that a problem? If I remove im getting error also.

class UserRegistration extends ActiveRecord
{
    public $username;
    public $firstname;
    public $lastname;
    public $email;
    public $Country;
    public $State;
    public $City;
    public $phone_number;
    public $about_me;
    public $password;
    public $verify_password;    
    public $photo;

    public static function tableName()
    {
        return 'user';
    }

    public function rules()
    {
        return [
            [['username','password', 'email'], 'required'],
            [['status', 'created_at', 'updated_at', 'teespring_id'], 'integer'],
            [['username','lastname','firstname', 'password_hash', 'password_reset_token', 'email','about_me'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['role'], 'string', 'max' => 10],
            [['email','username'],'email'],
            [['email','username','password_reset_token'], 'unique'],
            ['phone_number','string','length'=>10],
            [['Country','State','City'],'string'],
            ['verify_password','compare','compareAttribute'=>'password'],
            [['photo'],'file', 'extensions' => 'png, jpg']
        ];
    }
}

Update: I have tried $model = UserRegistration::findOne($id) also.no luck Please help me.

Upvotes: 0

Views: 911

Answers (4)

lalithkumar
lalithkumar

Reputation: 3540

Finally i got the solution.I have created two findModel and rendered to the form.

public function actionProfile(){

            $model      = $this->findModel(Yii::$app->user->identity->id);
            $profile    = $this->findProfileModel(Yii::$app->user->identity->id);
            return $this->render('/userregister/update', [
                'model' => $model,
                'profile'=>$profile,
            ]);
    }

Thanks for all.

Upvotes: 0

FeR-S
FeR-S

Reputation: 339

Its look strange. This model looks like a ModelForm for User registration and it doesn`t need to have a table.

I think you have a User model also, so u should try to User::findOne($id)

If you have just one model for User and Registration, then you should not set attributes as public if it in table.

So you need just public $verify_password, because u dont have this row in table i guess. And i think you dont have the photo row in table, because usually photos name is user_id. In that case u should set it as public too.

Can u get all your rows from User table so i can create a right model for u?

Upvotes: 1

Fabrizio Caldarelli
Fabrizio Caldarelli

Reputation: 3008

This is wrong.

$model = UserRegistration::find($id)->one()

It should be:

$model = UserRegistration::findOne($id);

or

$model = UserRegistration::find()->where(['id' => $id])->one();

if $id is referred to 'id' field

Upvotes: 1

Bizley
Bizley

Reputation: 18021

It's UserRegistration::findOne($id) and you must not declare attributes that are coming from database because these will be empty.

Upvotes: 1

Related Questions