zxbodya / yii2-gallery-manager

93 stars 61 forks source link

Error in actionAjaxUpload #6

Open BrunoAl25 opened 9 years ago

BrunoAl25 commented 9 years ago

When i add an image for upload gives the error "Trying to get property of non-object" in the line $image = $this->behavior->addImage($fileName)

I check it out in FireBug and i can see all the properties from the image , but in the addImage simples crash.

zxbodya commented 9 years ago

Looks weird... Likely you have something wrong in configuration... - can you show your code related to gallery manager?

BrunoAl25 commented 9 years ago

I created a simple table Call PRODUCT

CREATE TABLE IF NOT EXISTS tbl_product (
  id serial,
  product varchar(125) NOT NULL,  
  gallery_id int NOT NULL,
  file_name varchar(128) NOT NULL, 
  CONSTRAINT tbl_product_pkey PRIMARY KEY (id),    
  CONSTRAINT tbl_product_gallery_image_id FOREIGN KEY (gallery_id) REFERENCES tbl_gallery_image(id)  
);

After that ran the migration to create the table gallery_image.

This is the files:

-- Product model --

<?php

namespace app\models;

use Yii;
use zxbodya\yii2\galleryManager\galleryBehavior;

/**
 * This is the model class for table "{{%product}}".
 *
 * @property integer $id
 * @property string $product
 * @property integer $gallery_id
 * @property string $file_name
 *
 * @property GalleryImage $gallery
 */
class Product extends \yii\db\ActiveRecord
{
    public $image;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%product}}';
    }

    public function behaviors()
    {
        return [
             'galleryBehavior' => [
                 'class' => GalleryBehavior::className(),
                 'type' => 'product',
                 'extension' => 'jpg',
                 'directory' => Yii::getAlias('@webroot') . '/images/product/gallery',
                 'url' => Yii::getAlias('@webroot') . '/images/product/gallery',
                 'versions' => [
                     'small' => function ($img) {
                         /** @var ImageInterface $img */
                         return $img
                             ->copy()
                             ->thumbnail(new Box(200, 200));
                     },
                     'medium' => function ($img) {
                         /** @var ImageInterface $img */
                         $dstSize = $img->getSize();
                         $maxWidth = 800;
                         if ($dstSize->getWidth() > $maxWidth) {
                             $dstSize = $dstSize->widen($maxWidth);
                         }
                         return $img
                             ->copy()
                             ->resize($dstSize);
                     },
                 ]
             ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['product'], 'required'],
            [['gallery_id'], 'integer'],
            [['product'], 'string', 'max' => 125],
            [['file_name'], 'string', 'max' => 128]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'product' => 'Product',
            'gallery_id' => 'Gallery ID',
            'file_name' => 'File Name',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGallery()
    {
        return $this->hasOne(GalleryImage::className(), ['id' => 'gallery_id']);
    }
}

-- Product Controller

<?php

namespace app\controllers;

use Yii;
use app\models\Product;
use app\models\ProductSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use zxbodya\yii2\galleryManager\GalleryManagerAction;

/**
 * ProductController implements the CRUD actions for Product model.
 */
class ProductController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
           'galleryApi' => [
               'class' => GalleryManagerAction::className(),
               // mappings between type names and model classes (should be the same as in behaviour)
               'types' => [
                   'product' => Product::className()
               ]
           ],
        ];
    }

    /**
     * Lists all Product models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ProductSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Product model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

     /**
     * Creates a new Product model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actiongalleryApi()
    {
        echo "OK";
    }

    /**
     * Creates a new Product model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Product();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Product model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Product model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Product model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Product the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Product::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}

-- Product Form --

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use zxbodya\yii2\galleryManager\GalleryManager;

/* @var $this yii\web\View */
/* @var $model app\models\Product */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="product-form">

      <?php $form = ActiveForm::begin(['options' => ['class'=>'form-horizontal','enctype' => 'multipart/form-data']]); ?>

    <?= $form->field($model, 'product')->textInput(['maxlength' => 125]) ?>

    <?= $form->field($model, 'gallery_id')->textInput() ?>

    <?= $form->field($model, 'file_name')->textInput(['maxlength' => 128]) ?>
    <?php 

        if ($model->isNewRecord) {
            echo 'Can not upload images for new record';
        } else {
            echo GalleryManager::widget(
                [
                    'model' => $model,
                    'behaviorName' => 'galleryBehavior',
                    'apiRoute' => 'product/galleryApi'
                ]
            );
        }
    ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
zxbodya commented 9 years ago

Looks like I found a bug: use zxbodya\yii2\galleryManager\galleryBehavior;

galleryBehavior should be GalleryBehavior