Yii2 在非对象上调用成员函数 saveAs()
Yii2 Call to a member function saveAs() on a non-object
所以我通过 doingiteasychannel 在 youtube 上学习了 Yii2 教程,但是在单击提交按钮时出现以下错误。
Call to a member function saveAs() on a non-object
下面是控制器操作中的一些代码我已经创建了表单并添加了 enctype 但仍然给出错误。
下面的代码应该将文件保存到一个目录,然后将文件的路径添加到我的 table.
的头像栏中
$userprofile = UserProfile::findOne(['user_id' => $id]);
$imageName = $user->username;
$userprofile->file = UploadedFile::getInstance($userprofile, 'file');
$userprofile->file->saveAs('uploads/'.$imageName.'.'.$userprofile->file->extension);
$userprofile->avatar = 'uploads/'.$imageName.'.'.$userprofile->file->extension;
$userprofile->user_id = Yii::$app->user->id;
$userprofile->save(false);
请在您的 UserProfile
模型中声明一个 属性(变量)file
并在 rules()
方法中分配其验证规则 - reference
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "user". (for eg)
*
* ...
*/
class UserProfile extends \yii\db\ActiveRecord
{
// for file uploading
public $file;
public function rules()
{
return [
// ... other attribute validation rules
[['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user'; // for eg
}
// ... rest of the code
所以我通过 doingiteasychannel 在 youtube 上学习了 Yii2 教程,但是在单击提交按钮时出现以下错误。
Call to a member function saveAs() on a non-object
下面是控制器操作中的一些代码我已经创建了表单并添加了 enctype 但仍然给出错误。
下面的代码应该将文件保存到一个目录,然后将文件的路径添加到我的 table.
的头像栏中$userprofile = UserProfile::findOne(['user_id' => $id]);
$imageName = $user->username;
$userprofile->file = UploadedFile::getInstance($userprofile, 'file');
$userprofile->file->saveAs('uploads/'.$imageName.'.'.$userprofile->file->extension);
$userprofile->avatar = 'uploads/'.$imageName.'.'.$userprofile->file->extension;
$userprofile->user_id = Yii::$app->user->id;
$userprofile->save(false);
请在您的 UserProfile
模型中声明一个 属性(变量)file
并在 rules()
方法中分配其验证规则 - reference
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "user". (for eg)
*
* ...
*/
class UserProfile extends \yii\db\ActiveRecord
{
// for file uploading
public $file;
public function rules()
{
return [
// ... other attribute validation rules
[['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user'; // for eg
}
// ... rest of the code