限制条目数量,并在 for 循环中断后重新启动。

huangapple go评论56阅读模式
英文:

limit the number of entries and restart after break in the for loop

问题

I have a model and ready-made data in a table. In this model, I added a new field and made a connection with another table.

And in order not to manually fill in these fields for each record, I want to create a migration that will automatically fill in this field for all records.

Relationship table has two fields: post_id and author_id.

I am trying to do like this:

$posts = Posts::find()->all();

foreach ($posts as $index => $post) {
    for($i = 0, $j = 0; $i < $index; $j++, $i++) {
        if ($j >= 4) {
          break;
        }
        $item = new PostAuthor();
        $item->setAttribute('post_id', $post->id);
        $item->setAttribute('author_id', $j + 1);
        $item->save();
    }
}

Or:

$posts = Posts::find()->all();

foreach ($posts as $index => $post) {
    for($i = 0; $i < $index && $i < 4; $i++) {
        $item = new PostAuthor();
        $item->setAttribute('post_id', $post->id);
        $item->setAttribute('author_id', $i + 1);
        $item->save();
    }
}

But I get the same, only when j reaches 4, the cycle does not start again, and it adds everything else from 1 to 4.

Is it possible to make the loop start over for the post IDs I'm circling in the screenshot?

In idea my table should look like this.

英文:

I have a model and ready-made data in a table. In this model, I added a new field and made a connection with another table.

And in order not to manually fill in these fields for each record, I want to create a migration that will automatically fill in this field for all records.

Relationship table has two fields: post_id and author_id.

I am trying to do like this:

$posts = Posts::find()-&gt;all();

foreach ($posts as $index =&gt; $post) {
    for($i = 0, $j = 0; $i &lt; $index; $j++, $i++) {
        if ($j &gt;= 4) {
          break;
        }
        $item = new PostAuthor();
        $item-&gt;setAttribute(&#39;post_id&#39;, $posts-&gt;id);
        $item-&gt;setAttribute(&#39;author_id&#39;, $j + 1);
        $item-&gt;save();
    }
}

Or:

$posts = Posts::find()-&gt;all();

foreach ($posts as $index =&gt; $post) {
    for($i = 0; $i &lt; $index &amp;&amp; $i &lt; 4; $i++) {
        $item = new PostAuthor();
        $item-&gt;setAttribute(&#39;post_id&#39;, $posts-&gt;id);
        $item-&gt;setAttribute(&#39;author_id&#39;, $i + 1);
        $item-&gt;save();
    }
}

But I get the same, only when j reaches 4, the cycle does not start again, and it adds everything else from 1 to 4:

限制条目数量,并在 for 循环中断后重新启动。

Is it possible to make the loop start over for the post IDs I'm circling in the screenshot?

In idea my table should look like this

限制条目数量,并在 for 循环中断后重新启动。

答案1

得分: 1

$posts = Posts::find()->all();
$authors = [1,2,3,4]; // 作者的ID
$i = 1;
foreach($posts as $post){
$postAuthors = array_slice($authors, 0, $i);
$post->authors()->sync($postAuthors); // 假设你的关系是belongsToMany(),否则你可以使用foreach $postAuthors来创建PostAuthor
$i++;
// 你也可以使用 $i+=rand(1,4) 来增加更多的随机性
if($i>4){
$i=0; // 设置为0将使每5篇文章不包含作者。如果设置为1,所有文章都将包含作者
}
}
对于每篇文章,我们选择N个作者,同步关系,然后重置计数器。

英文:
$posts = Posts::find()-&gt;all();
$authors = [1,2,3,4]; //ids of the authors
$i = 1;
foreach($posts as $post){
  $postAuthors = array_slice($authors, 0, $i);
  $post-&gt;authors()-&gt;sync($postAuthors); //I assume your relation is belongsToMany(), otherwise you can foreach $postAuthors to create PostAuthor
  $i++;
  //you can use $i+=rand(1,4) instead to add more randomness
  if($i&gt;4){
    $i=0; //settings to 0 will produce each 5 post to not have authors. If you set to 1 all posts will have author
  }
}

For each post we take N authors, sync relation then reset counter

huangapple
  • 本文由 发表于 2023年2月7日 04:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定