S
S
sdgs4s4 .2017-09-21 11:07:11
Yii
sdgs4s4 ., 2017-09-21 11:07:11

Why getImage() - Yii2 swears?

Valar Morghulis!

Controller

<?php

namespace app\controllers;
use app\models\About;
use Yii;

class AboutController extends AppController
{
    public function actionView()
    {
        $about = About::find()->where(['status' => '1'])->limit(6)->all();
        $this->setMeta('E-SHOPPER О Компании');
        return $this->render('view', compact('about'));
    }
}


Model

<?php

namespace app\models;
use yii\db\ActiveRecord;


class About extends ActiveRecord{

    public function behaviors()
    {
        return [
            'image' => [
                'class' => 'rico\yii2images\behaviors\ImageBehave',
            ]
        ];
    }

    public static function tableName(){
        return 'about';
    }
}


View

<?php foreach($about as $about2): ?>
                            
                            <?php $mainImg = $about->getImage();?>
                            <?= Html::img($mainImg->getUrl('268x249'), ['alt' => $about->name])?>
                            
                            <p><?= $about2->content?></p>
                            
                        <?php endforeach;?>


$contentloads, and if I insert an image, then the next. mistake:
Call to a member function getImage() on array

Cards (zhmyak)

4916790209214d26b8151bbba6d5fdfb.PNG

What could be wrong? I've been poking for 5 hours already and all to no avail

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-09-21
@Encoderast

$about->getImage()

You are not accessing an object, but an array, and the array does not have a getImage () method, apparently you need to access $about2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question