A
A
Alex Mirgorodskiy2017-09-27 17:41:29
Yii
Alex Mirgorodskiy, 2017-09-27 17:41:29

How to get data from many-to-many tables?

Friends, do not tell me how to correctly compose a request in order to AJAX-ohm to get data from tables related to one common (many-to-many relationship).
I pass the hashtag id AJAX, and look in the transit (connected) table for the id-s of all the news, then with an array of these IDs I try to get all the news from the news table, with the id data , select news with the specified id);
Don't tell me how? Or, in general, maybe everything is simpler, and is it possible without a bicycle? here is the code

if (Yii::$app->request->post('hesh_id')) {

//id хештега
                $id = Yii::$app->request->post('hesh_id');

//id новостей с таким хештегом (из связной таблицы)
                $news_tags = NewsTags::find()->select('news_id')->where(['tags_id' => $id])->all();

//Запихиваю все в один массив
                foreach ($news_tags as $news_tag)
                    {
                         $news_id[] = $news_tag->news_id;
                     }


//Привожу к виду 1, 2, 3
                $comma_separated = implode(",", $news_id);

//И делаю запрос 
                $news = News::find()->where(['in', 'id', [$comma_separated]])->all();

                 return debug($news);

Here is such a clumsy construction that displays the news only with the first id-shnik;
Strongly do not swear, with similar queries Where, in and similar for the first time I communicate as well as many-to-many tables.
Do not tell me how to get to all their news easier through the id tag?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Mirgorodskiy, 2017-09-27
@AlexWeb6667

In short, I don’t know if I did worse) But I decided the matter this way, with feedback.

if (Yii::$app->request->isAjax) {

            if (Yii::$app->request->post('hesh_id')) {

                $id = Yii::$app->request->post('hesh_id');

                $tags_id = Tags::findOne($id);

                $this->layout = false;
                return $this->render('ajax', compact('tags_id'));

            }

        }

and in the view, I displayed all the news of this tag through the link.
<?php $news = $tags_id->news ?>


<?php foreach ($news as $new): ?>

        <?php $main_img = $new->getImage()->getUrl(); ?>

<div class="news_container">
    <div  class="news_img_cont" style="background: url('<?= $main_img ?>') center center no-repeat;background-size: cover;"><!--*/
/**/
/*        --><img class="news_layout" id="news_lu" src="/web/img/bg/map_frame_lu.svg" alt=""><!--*/
/*        --><img class="news_layout" id="news_lb" src="/web/img/bg/map_frame_lb.svg" alt=""><!--*/
/*        --><img class="news_layout" id="news_rb" src="/web/img/bg/map_frame_rb.svg" alt=""><!--*/
/*        --><img class="news_layout" id="news_ru" src="/web/img/bg/map_frame_ru.svg" alt=""><!--*/
/*        --><div data-id = "*/<?//= $new->id ?><!--" class="fade_new"></div>-->
         </div>

    <div class="text_container">
    <h1 class="title_new"><?= $new->name ?></h1>
    <div class="tags_news">
        <img src="/web/img/ICO/news/tag.svg" alt="" >
        <?php $tags_c = count($new->tags); if ($tags_c == 0): ?>

            <span >Разное</span>


        <?php else: ?>

            <? $i = 0; ?>
            <?php foreach ($new->tags as $tag):?>
                <span  data-id = "<?= $tag->id ?>"><?= $tag->name ?><?=count($new->tags) != ++$i ? "," : ""?></span>
            <?php endforeach; ?>

        <?php endif; ?>
    </div>
    <p class="text_news"><?php $text = $new->description ?>
        <?php if (strlen($text)>280)
        {
            $text = substr ($text, 0,strpos ($text, " ", 280)); echo $text . " ...";
        }
        else echo $text; ?>
    </p>

        <span class="data"> <?php Yii::$app->formatter->locale = 'ru-RU';
            echo Yii::$app->formatter->asDate($new->date); // выведет: 1 января 2014 г. ?></span>
    </div>
</div>

<?php endforeach; ?>

who can be useful as a friend ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question