Yii загрузка и вывод картинок?

Общие вопросы по использованию фреймворка. Если не знаете как что-то сделать и это про Yii, вам сюда.
Ответить
Аватара пользователя
des1roer
Сообщения: 391
Зарегистрирован: 2015.02.06, 17:03
Контактная информация:

Yii загрузка и вывод картинок?

Сообщение des1roer »

делаю по этому
http://belyakov.su/yii-magazin-3_1-dopilivaem-book
но блин какая то лажа получается.
использую это расширение http://www.yiiframework.com/extension/image-column
в модели

Код: Выделить всё

    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name', 'safe'),
                        array('link', 'unsafe'),
                        array('link', 'file',
                            'types'=>'jpg, gif, png, pdf',
                            'maxSize'=>1024 * 1024 * 5, // 5 MB
                            'allowEmpty'=>'true',
                            'tooLarge'=>'Файл весит больше 5 MB. Пожалуйста, загрузите файл меньшего размера.',
                            ),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, link', 'safe', 'on'=>'search'),
        );
    }
в форме

Код: Выделить всё

    <div class="row">
        <?php echo $form->labelEx($model,'link'); ?>
        <?php echo $form->fileField($model, 'link'); 
                if(!$model->isNewRecord) {
                    echo CHtml::image(Yii::app()->baseUrl.'/images/pic/' .$model->link, $model->name, array('height'=>100));
                        }
?>
        <?php echo $form->error($model,'link'); ?>
    </div>
во вьюшке

Код: Выделить всё

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'img-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        array(
            'class' => 'EImageColumn',
            'header' => 'картинка',
            'name' => 'link',
            'pathPrefix' => Yii::app()->baseUrl . "/images/pic/",
            'htmlOptions' => array('style' => 'width: 150px;'),
        ),
        'name',
        array(
            'class' => 'CButtonColumn',
        ),
    ),
));
?>
и страшный контролер

Код: Выделить всё

    public function actionCreate()
    {
        $model = new Img;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if (isset($_POST['Img']))
        {
            $model->attributes = $_POST['Img'];
            $command = Yii::app()->db->createCommand();
                    $max = Yii::app()->db->createCommand()
                    ->select('max(id) as max')
                    ->from('img')
                    ->queryScalar();
                    
            $file_image = CUploadedFile::getInstance($model, 'link');
            if (is_object($file_image) && get_class($file_image) === 'CUploadedFile')
                $model->link = $file_image;
            if ($model->save())
            {       
                if (is_object($file_image))
                {
                    $model->link->saveAs($_SERVER['DOCUMENT_ROOT'].'/techbase/images/pic/' .($max+1).'.'.$file_image->extensionName);
                   
                }
                  $command->update('img', array(
                            'link' => ($max+1).'.'.$file_image->extensionName,                      
                      ),'id=:id', array(':id'=>$max+1)
                          );
                 $model->link = $max.'.'.$file_image->extensionName;
                $this->redirect(array('view', 'id' => $model->id));
            }
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }
как можно поправить контролер. или пусть так будет?
Ответить