英文:
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()->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', $posts->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', $posts->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
答案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()->all();
$authors = [1,2,3,4]; //ids of the authors
$i = 1;
foreach($posts as $post){
$postAuthors = array_slice($authors, 0, $i);
$post->authors()->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>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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论