【发布时间】:2016-01-31 15:00:19
【问题描述】:
我想知道是否有人可以为我指出正确的方向,如何让用户的所有朋友的帖子合并某种新闻流
我有一个简单的用户实体 $user->getPosts() 和 $user->getFriends() 它通过学说中的相应关系设置返回适当的帖子/用户对象。这些工作正常,但我如何将我所有朋友的所有帖子编译成一个 ArrayCollection 帖子?
我能想到的唯一方法是遍历所有朋友,获取帖子,将它们合并到一个数组中,然后执行一些排序...但这听起来有点复杂/性能不佳
我不认为 $user->getFriends()->getPosts() 也可以简单地进行,我将如何正确排序帖子以获得唯一最新的帖子?我也想知道性能问题。给定一个用户有很多朋友,从数据库中检索到的数据会非常多。有效地征服它的最佳方法是什么?还是因为 symphony 的延迟加载,我根本不担心这个?
感谢您的帮助!
这是用户实体
AppBundle\Entity\User:
type: entity
repositoryClass: AppBundle\Entity\UserRepository
table: user
indexes:
id:
columns:
- id
uniqueConstraints:
username:
columns:
- username
handle:
columns:
- handle
id:
id:
type: integer
nullable: false
options:
unsigned: true
id: true
generator:
strategy: IDENTITY
fields:
username:
type: string
nullable: false
length: 30
options:
fixed: false
password:
type: string
nullable: false
length: 60
options:
fixed: false
email:
type: string
nullable: false
length: 60
options:
fixed: false
oneToMany:
images:
targetEntity: Image
mappedBy: user
posts:
targetEntity: Post
mappedBy: user
postcomments:
targetEntity: Postcomment
mappedBy: user
oneToOne:
sedcard:
targetEntity: Sedcard
mappedBy: user
portfolio:
targetEntity: Portfolio
mappedBy: user
manyToMany:
myFriends:
targetEntity: User
joinTable:
name: friend
joinColumns:
user_source:
referencedColumnName: id
inverseJoinColumns:
user_target:
referencedColumnName: id
inversedBy: friendsWithMe
friendsWithMe:
targetEntity: User
mappedBy: myFriends
lifecycleCallbacks: { }
这里是帖子实体
AppBundle\Entity\Post:
type: entity
table: post
indexes:
user_id:
columns:
- user_id
id:
id:
type: integer
nullable: false
options:
unsigned: true
id: true
generator:
strategy: IDENTITY
fields:
userId:
type: integer
nullable: false
options:
unsigned: true
column: user_id
body:
type: text
nullable: false
length: 65535
options:
fixed: false
type:
type: boolean
nullable: false
created:
type: datetime
nullable: false
active:
type: boolean
nullable: false
oneToMany:
postcomments:
targetEntity: Postcomment
mappedBy: post
manyToOne:
user:
targetEntity: User
inversedBy: posts
joinColumn:
name: user_id
referencedColumnName: id
lifecycleCallbacks: { }
【问题讨论】:
-
扩展实体并编写自己的函数。