0

I don't. understand where is error in my code. But I think in init() function!

var_dump ($this->newsType());

No method or closure named "my" was found in the NewsStream class and its behaviors.

This is NewsStream.php (widget class)

public function init()
{
    $data = [
        'content' => [],
        'error' => 'Модуль отключен.',
    ];


    if (config('news_stream.allow') == 0) {
        $data = cache()->get(CacheNames::NEWS_STREAM);

        if ($data === false) {
            $data = [];

            try {
                // Подключаюсь к БД
                $this->db = Yii::createComponent([
                    'class' => 'CDbConnection',
                    'connectionString' => 'mysql:host=127.0.0.1;port=3306;dbname=la2worldweb',
                    'enableProfiling' => YII_DEBUG,
                    'enableParamLogging' => true,
                    'username' => 'root',
                    'password' => 'xdemonx1234',
                    'charset' => 'utf8',
                    'emulatePrepare' => true,
                    'tablePrefix' => 'ghtweb',
                ]);

                app()->setComponent('NewsStreamDb', $this->db);

                
                $newsType = config('news_types.type');

                if (method_exists($this, $newsType)) {
                    $data['content'] = $this->$newsType();

                    foreach ($data['content'] as $k => $v) {
                        $data['content'][$k]['id'] = $this->getNewsLink($v['id']);
                        $data['content'][$k]['title'] = $this->getNewsTitle($v['title']);
                        $data['content'][$k]['updated_at'] = $this->getLastDate($v['updated_at']);
                        $data['content'][$k]['user_id'] = $this->getAuthorId($v['author_id']);
                    }

                    if (30) {
                        cache()->set(CacheNames::NEWS_STREAM, $data, 30 * 60);
                    }
                } else {
                    $data['error'] = 'Метод для обработки форума не найден.';
                }
                
            } catch (Exception $e) {
                $data['error'] = $e->getMessage();
            }
        }
    }

    app()->controller->renderPartial('//news_stream', $data);
}

This is call of that widget

<div class="icon" id="icon_blue1"></div>
<div class="line_blue">
<h1 class="line">Новости </h1>

<?php $this->widget('app.widgets.NewsStream.NewsStream') ?>


</div>

That's catching of error

enter image description here

debug

niac
  • 33
  • 2
  • 17

1 Answers1

0

You are trying to access a method or attribute named my on the NewsStream class. This class doesn't seem to have any attribute or get method with this name.

Since there is no code with this type of call in your examples, I assume it is being done on another place, so please check your code (especially forms give this error) for an attempt to access something like $newsStreamModel->my.

Also, is not a good thing to create the CDbConnection component inside a widget. This should be done with configuration files, read the official docs for more info: Data Access Objects (DAO)

Also (again), don't do app()->controller->renderPartial inside the widget. Just "echo" the output and leave the render to your controller. It is like your widget is trying to do the controller's job. If someday you need to use this widget in another controller, with another kind of code, you might not be able.

bpanatta
  • 509
  • 3
  • 12