Php 与动态实体laravel 4的关系
我在这个场景中遇到了一个大问题: 我正在构建一个简单的帖子和评论系统,但不同的是,我有两个主要实体,它们是: -使用者 -团队 所以我有两张表: 表格用户:Php 与动态实体laravel 4的关系,php,database,laravel,laravel-4,Php,Database,Laravel,Laravel 4,我在这个场景中遇到了一个大问题: 我正在构建一个简单的帖子和评论系统,但不同的是,我有两个主要实体,它们是: -使用者 -团队 所以我有两张表: 表格用户: id | name | surname | fullname | image_profile | 1 jhon smith jhon smith apple.jpg 桌面团队 id | fullname | image_profile | 1 BKA Sport three.jpg
id | name | surname | fullname | image_profile |
1 jhon smith jhon smith apple.jpg
桌面团队
id | fullname | image_profile |
1 BKA Sport three.jpg
每个用户都可以为团队发布帖子或评论
因此,我已经设置了一个列来区分海报的实体,如下所示
表格帖子
id | title_post | body | poster_id | poster_type | wall_id | wall_type
1 test woo 1 User 1 Team
2 test 2 excellent 1 Team 1 User
3 test3 perfect 1 Team 1 Team
正如你所见,我甚至设置了发布此帖子的专栏墙,在本例中,ID为1的用户在ID为1的团队的墙上写了一篇帖子
团队已经在ID为1的用户的墙上写下了。
在第三场比赛中,球队在自己的墙上留下了一个门柱
现在,如果我想选择“实体或海报类型”团队的所有墙柱,将其关联到正确的实体,我必须如何建立关系
现在,我只是做了一个没有关系的肮脏解决方案,但将结果与数组合并,我认为这对服务器来说很沉重,但我没有找到任何使用关系的方法。我甚至想过使用多态关系,但它似乎做得不好
这是我目前的表现
<?php
$posts = Post::where('wall_id','=','1')->where('wall_type','=','Team')->get()->toArray();
// I get just the id of the current posters
foreach ($posts as $key => $value) {
if ($value["poster_type"] == "User") {
$poster['User'][$key]['id'] = $value['poster_id'];
}
if ($value['poster_type'] == "Team") {
$poster['Team'][$key]['id'] = $value['poster_id'];
}
}
// all ids of the users
$user_ids = array_fetch($poster['User'],'id');
// all ids of the teams
$team_ids = array_fetch($poster['Team'],'id');
$users_informations = User::whereIn('id',array_unique($user_ids))->get()->toarray();
$teams_informations = User::whereIn('id',array_unique($team_ids))->get()->toarray();
// Here i merge the results
你应该检查一下,因为它看起来正是你想要的。啊,很抱歉,我看到你说你知道多态关系,但它们不适合你的用例。也许再看一眼以防万一,但是的,也许你正在做的事情超出了他们的能力。谢谢你的评论,我会照你的建议去做。希望这只是我建立多态关系的错误。