S
S
Sergey Beloventsev2017-03-02 16:57:25
Yii
Sergey Beloventsev, 2017-03-02 16:57:25

Why isn't the behavior method called?

here is the behavior

<?php
namespace common\components\behaviors;
use yii\base\Behavior;
class MenuFrontendBehavior extends Behavior
{
    public $alias;

    public $model;

    public $path;

    public $feild_alias;

    public function getPath(){
        return 'yes';
    }

    public function getModel(){
        $model=$this->model;
    }

}

so I connect to the model
use common\components\behaviors\MenuFrontendBehavior;

class Firmcat extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [

                [
                    'class' => SluggableBehavior::className(),
                    'attribute' => 'title',
                    'slugAttribute' => 'slug'
                ],
                'menu' => [
                    'class' => MenuFrontendBehavior::className(),
                    //'model' => 'yes',
                    //'prop2' => 'value2',* /
                ]

        ];
    }

then in to the controller I try to call like this
use common\models\Firmcat;
use yii\web\Controller;
class MenucreateController extends Controller
{

    public function actionIndex(){
        $model=Firmcat::find()->with('image')->all();
        return $this->render('index',[
            'path'=>$model->getPath()
        ]);
    }
}

I get an error
Call to a member function getPath() on array

explain why

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mitaichik, 2017-03-02
@Sergalas

Because the line $model=Firmcat::find()->with('image')->all(); Selects an array of models for you, rather than a specific model on which to call a method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question