S
S
Svetlana Galenko2018-11-04 21:22:38
Yii
Svetlana Galenko, 2018-11-04 21:22:38

When displaying news on the site, it gives an error Call to undefined method app\models\SystemNewsRecord::find()?

Greetings, I think I ran into a trifling error, but I can’t think of what could be the reason. When displaying an article on the site, it gives:

Call to undefined method app\models\SystemNewsRecord::find()
1. in C:\Users\User\OSPanel\domains\medicalyii2\controllers\NewsController.php at line                               
 ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
    public function actionIndex() {
        //return array => object
    	$model = SystemNewsRecord::find()->where(['hide' => 'show'])->orderBy(['put_date' => SORT_DESC])->all();
        return $this->render('index', [
            'model' => $model
        ]);
    }
    public function actionView()
    {
        $id_news = Yii::$app->request->get('id');
        //object
        $model = SystemNewsRecord::findOne($id_news);
                
2. yii\base\ErrorHandler::handleFatalError()

Here is my controller, from where it gives this error:
public function actionIndex() {
        //return array => object
    	$model = SystemNewsRecord::find()->where(['hide' => 'show'])->orderBy(['put_date' => SORT_DESC])->all();
        return $this->render('index', [
            'model' => $model
        ]);
    }
    public function actionView()
    {
        $id_news = Yii::$app->request->get('id');
        //object
        $model = SystemNewsRecord::findOne($id_news);
        if (!$model)
            throw new NotFoundHttpException('Данная новость не найдена');
        return $this->render('view', [
        	'model' => $model
        ]);
    }

Here is the model:
public static function tableName()
    {
        return 'system_news';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'body', 'alt', 'img_small', 'img_big', 'put_date', 'hide'], 'required'],
            [['body', 'alt', 'img_small', 'img_big', 'hide'], 'string'],
            [['put_date'], 'safe'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id_news' => 'Id News',
            'name' => 'Name',
            'body' => 'Body',
            'alt' => 'Alt',
            'img_small' => 'Img Small',
            'img_big' => '',
            'put_date' => 'Put Date',
            'hide' => 'Hide',
        ];
    }

view:
<?php foreach ($model as $news): ?>

                        <div class="news-sec">
                            <?= Html::img('@web/files/news/'.$news->img_small, [
                                'alt' => $news->alt,
                                'title' => $news->alt,
                            ]) ?>
                            <div class="detail">
                                <span><?= $news->put_date ?></span>
                                <h3><?= Html::a($news->name, ['view', 'id' => $news->id_news]) ?></h3>

                                <p><?= StringHelper::truncateWords($news->body, 30) ?></p>

                                <?= Html::a('читать далее', [
                                    'view',
                                    'id' => $news->id_news
                                ], ['class' => 'read-more']) ?>
                            </div>
                        </div>

                        <?php endforeach; ?>

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Andrey, 2018-11-05
@VladimirAndreev

So there is no find method in the model ... Is there a Query for it at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question