如何在学说中插入非常的参考

时间:2021-04-14 13:45:54

I have 3 entities: User, Page, Post

我有3个实体:用户,页面,帖子

In the Post entity:

在Post实体中:

class Post
{
    /**
     * @ORM\ManyToOne(targetEntity="Page")
     */
    private $page;

    /**
     * @var ArrayCollection $subscribers Specify users that see this post.
     *
     * @ORM\ManyToMany(targetEntity="User", inversedBy="wallPosts")
     */
    private $subscribers;
}

In the Page enity:

在页面中:

class Page
{
    /**
     * @ORM\ManyToMany(targetEntity="User")
     */
    private $members;
}

When a post is published I want that all users that are members of Page are set as subsribers of Post

发布帖子时,我希望所有作为Page成员的用户都被设置为Post的子成员

$post->setSubscribers($post->getPage()->getMembers())

note: Page can have several thousand users, what is best way that have best performance? Should I use a native query, database triggers or procedures?

注意:页面可以拥有数千个用户,具有最佳性能的最佳方式是什么?我应该使用本机查询,数据库触发器还是程序?

1 个解决方案

#1


1  

You can do it in cycle. Subscribe 50 users per one iteration to new post. Do not forget to clean doctrine internal storage. The main idea is

你可以循环做。每次迭代订阅50个用户到新帖子。不要忘记清理学说内部存储。主要想法是

$page = $post->getPage();
for ($i = 1;; ++$i) {
  $members = $page->getPagindatedMembers($i);
  if (empty($members)) {
     break;
  }
  foreach ($members as $member) {
     $post->addSubscriber($member);
  }
  $em->flush();
  $em->clear(Member::class);
}

#1


1  

You can do it in cycle. Subscribe 50 users per one iteration to new post. Do not forget to clean doctrine internal storage. The main idea is

你可以循环做。每次迭代订阅50个用户到新帖子。不要忘记清理学说内部存储。主要想法是

$page = $post->getPage();
for ($i = 1;; ++$i) {
  $members = $page->getPagindatedMembers($i);
  if (empty($members)) {
     break;
  }
  foreach ($members as $member) {
     $post->addSubscriber($member);
  }
  $em->flush();
  $em->clear(Member::class);
}