0
這個上傳圖片空是我的_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);
}',
],
]);
?>
我的文件在我的主機上傳,但我需要的是URL
這是我的產品控制器和形象爲空:
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']],
];
}
我怎樣才能獲得圖像URL與$模型 - >圖像在控制器!?
名 「:」 PHP的警告 「 」消息「: 」提供的foreach()無效參數「, 」代碼「:2 」類型「:」 YII \\基地\\ ErrorException「,」file「:」D:\\ xampp71 \\ htdocs \\ crmMaster \\ models \\ Product.php「,」line「:102,」stack-trace「:[」#0 D:\\ xampp71 \\ htdocs \\ crmMaster \\ models \\ Product.php(102):yii \\ base \\ ErrorHandler-> handleError(2,'Invalid argumen ...','D:\\\\ xampp71 \\\ \ htdo ...',102,Array)「,」#1 D:\\ xampp71 \\ htdocs \\ crmMaster \\ controllers \\ ProductController.php(136):app \\ models \\ Product-> upload )「,」#2 – Saltern
var_dump(UploadedFile :: getInstance($ model,'image')); 死亡; 上傳後的結果是:null – Saltern
@Saltern您是否已將該代碼放入模型中?你必須在你加載()你的模型之前放置它。你的情況是'ProductController' – Pave