Answer the question
In order to leave comments, you need to log in
Yii2 I can't derive a static method from a model, it gives out Call to undefined method app\models\Category::model(), what's wrong?
I can't get a static method from the model, it gives out Call to undefined method app\models\Category::model()
Here is the model code
public static function All()
{
$models = self::model()->findAll();
$array = array();
foreach($models as $key)
{
$array[$key->id] = $key->title;
}
return $array;
}
public static function menu()
{
$models = self::model()->findAllByAttributes(array('position'=>$position));
$array = array();
if($position=='top')
{
$array[] = ['label' => '', 'url' => ['/page/index/id/1']];
}
foreach($models as $one)
{
$array[] = ['label' => '', 'url' => ['/page/index/id/'.$one->id]];
}
if($position=='top')
{
$array[] = Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username .')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']];
}
}
I'm trying to display in Layouts main.php like this
Category::menu('top')
The whole code is like this
<?php
NavBar::begin([
'options' => [
'class' => 'navbar-inverse',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-left'],
'items' => Category::menu('top')
]);
NavBar::end();
?>
But it always throws an error no matter what I do. Hope for help.
Answer the question
In order to leave comments, you need to log in
self::model() is Yii1 this static method is not present in yii2
You need to do
self::find() or findOne etc
Read the guide on this topic
https://github.com/yiisoft/yii2/blob/master/docs/ g...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question