yii2:为什么$model->;使用FileUploadUI上载图像后,图像为空

yii2:为什么$model->;使用FileUploadUI上载图像后,图像为空,yii,yii2,Yii,Yii2,这是my_form.php: <?php use dosamigos\fileupload\FileUploadUI; // with UI ?> <?= FileUploadUI::widget([ 'model' => $model, 'attribute' => 'image', 'url' => ['media/upload', 'id' => 'i

这是my_form.php:

   <?php


    use dosamigos\fileupload\FileUploadUI;


// with UI

    ?>
    <?=

    FileUploadUI::widget([
        'model' => $model,
        'attribute' => 'image',
        'url' => ['media/upload', 'id' => 'image'],
        'gallery' => false,
        'fieldOptions' => [
            'accept' => 'image/*'
        ],
        'clientOptions' => [
            'maxFileSize' => 200000
        ],
        // ...
        'clientEvents' => [
            'fileuploaddone' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
            'fileuploadfail' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
        ],
    ]);

    ?>

如何使用控制器中的$model->image获取图像url

您是否尝试过使用
yii\web\UploadedFile::getInstance()


name:“PHP警告”,“消息”:“为foreach()提供的参数无效”,“代码”:2,“键入”:“yii\\base\\ErrorException”,“文件”:“D:\\xampp71\\htdocs\\crmMaster\\models\\Product.PHP”,“行”:102,“堆栈跟踪”:[“#0 D:\\xampp71\\htdocs\\crmMaster\\models\\Product.PHP(102):yii\\base\\ErrorHandler->handleError”(2,'Invalid argumen…','D:\\\xampp71\\\\\htdo…',102,Array','1d:\\xampp71\\htdocs\\crmMaster\\controllers\\ProductController.php(136):app\\models\\Product->upload(),“\2var\u dump(UploadedFile::getInstance($model,'image'));死亡;上传后结果为:null@Saltern您是否已将该代码放入模型中?您必须在加载()之前将其放入模型中。在您的情况下,它是
ProductController
 public function actionCreate()
        {
        $request = Yii::$app->request;
        $model = new Product();
        $idCon = Yii::$app->user->id;

        $model->user_id = $idCon;
        if ($request->isAjax)
            {
            /*
             *   Process for ajax request
             */
            Yii::$app->response->format = Response::FORMAT_JSON;

            if ($request->isGet)
                {
                return [
                    'title' => "Create new Product",
                    'content' => $this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
                ];
                }
            else if ($model->load($request->post()) && $model->save())
                {

                return [
                    'forceReload' => '#crud-datatable-pjax',
                    'title' => "Create new Product",
                    'content' => '<span class="text-success">Create Product success</span>',
                    'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
                ];
                }
            else
                {
                return [
                    'title' => "Create new Product",
                    'content' => $this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
                ];
                }
            }
        else
            {
            /*
             *   Process for non-ajax request
             */
            if ($model->load($request->post()) && $model->save())
                {


                return $this->redirect(['view', 'id' => $model->id]);
                }
            else
                {
                return $this->render('create', [
                            'model' => $model,
                ]);
                }
            }

        }
use Yii;
use yii\web\UploadedFile;
use yii\base\Model;


/**
 * This is the model class for table "product".
 *
 * @property integer $id
 * @property string $name
 * @property string $price
 * @property string $image
 * @property string $url
 * @property integer $user_id
 *
 * @property ConProduct[] $conProducts
 * @property User $user
 * @property Sales[] $sales
 */
class Product extends \yii\db\ActiveRecord
    {


    /**
     * @inheritdoc
     */
    public static function tableName()
        {
        return 'product';

        }


    /**
     * @inheritdoc
     */
    public function rules()
        {
        return [
                [['price'], 'number'],
                [['user_id'], 'integer'],
                [['name', 'image'], 'string', 'max' => 300],
                [['url'], 'string', 'max' => 255],
                [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];

        }
$model->image = UploadedFile::getInstance($model, 'image');
if ($model->upload()) {
    // do some stuff with file
}