Widget and database

For two days I have been looking for an answer to my question, I began to get acquainted with yii2 a week ago). I am writing a widget where I connect to a table from a database
$ my = ‘Mybd’ :: find () -> all ();
return $ this-> render (‘Mywidget’, [‘bdr’ => $ my]);
Some people assure me that this is not possible, they do not offer a solution in exchange or a direction as needed. I ask for help with this issue, there is not enough knowledge on my own.

So… what’s the question?

sorry, how correctly to display data from the database in the widget)

Please state your issue as clearly as possible, or no one can give you any advice.

What have you done so far? What code have you written?

$ my = ‘Mybd’ :: find () -> all ();
return $ this-> render (‘Mywidget’, [‘bdr’ => $ my]);

What is this code snippet?

    class Mywidget extends Widget
  public function run()

  $my = 'Mybd'::find() -> all(); 

  return $ this-> render ('Mywidget',  ['widgetbd' =>$my]);	     

sorry my mistake.
My task is to display images from the database as a menu in the layout, unfortunately, I don’t know how to do it correctly, I decided using the widget, the query works as I need, but I think it’s wrong, but to solve This problem alone can not.

$my = 'Mybd'::find()->all();

I’m not sure if it’s OK or not, but we usually don’t quote a class name here.

$my = Mybd::find()->all();
return $this->render('Mywidget', ['widgetbd' => $my]);

Do you have a view script named ‘Mywidget.php’? The syntax you are using here requires a view script.

Try something like the following as a startup without a view script:

public function run()
    $bds = Mybd::find()->all();

    echo '<ul>\n';
    foreach($bds as $bd) {
        echo '<li>\n';
        echo '<p>Name = ' . $bd->name . '</p>\n';
        echo '<p>Description = ' . $bd->description . '</p>\n';
        echo '</li>\n';
    echo '</ul>\n';

I pass in the view widget, there is already html markup and foreach, everything works perfectly and as I need. But I was told in numerous forums that this is extremely not true, but how to do it right no one gave an answer) for this reason I turned to your forum because I couldn’t find any detailed information on this issue.

And what’s the problem right now?

I would like to know if it is permissible to connect to the database in a widget in this way…
Sorry, I’m an idiot … I just had to ask this question and not type all this nonsense above.

I think it’s completely OK. Why not?

I also don’t know why not. I’m satisfied because I’m new, I’m constantly asking on the forums, it’s said there that mvc breaks and I spent two days looking for an answer …

Thank you for your answer, my soul is calm)

I think that this widget should include the necessary db accessing inside it. Or you have to do it in each and every controller and pass the query result to the widget every time you want to use it. That’s sheer nonsense. Don’t give a damn to what MVC idiots say, if it will make things unnecessarily complicated.

1 Like

too sad I can’t like the same answer twice :slight_smile:

1 Like

To ask is the shame of one minute, and not to know is the shame of a lifetime. Be kinder to people who started to master the web 2 months ago) html, css, js, and php, their libraries and frameworks.

Thanks again for helping softark.

1 Like

I was really just reacting to the “Don’t give a damn to what MVC idiots say, if it will make things unnecessarily complicated.” advice and not trying to be mean to you by any ways! Sorry if you read that otherwise.

There is really no shame in asking while learning, and I’ll be glad to help you with your next question (that is, if @softark doesn’t beat me to it first)

1 Like

Thank you, @machour. I really appreciate your thumb up.

@FrelNewBew, I didn’t have any intention to insult you, my friend. I was just angry at the people who gave you a false advice to stick to MVC without showing you how to do it. They only know the word “MVC” but have no brain to think for themselves.

1 Like

sorry, it was addressed not to you) but to our dear friend) it turned out that he wasn’t for me and I wasn’t for you) in the end we all didn’t understand each other)

Good luck to all, thank you all))