我正在尝试实现类似于Ruby on Rails的多态关系的功能。我有以下三个表:
活动用户组织
事件可以是用户或组织的所有者,因此我的“事件”表包括以下列:owner_type和owner_id。
我可以通过内部连接和where子句轻松列出所有属于用户或组织的事件,但是,有一种方法可以根据owner_type列的值使连接表成为条件,允许将所有事件一起列出,不管owner_type?
我希望这是有道理的。
任何建议表示赞赏。
谢谢。
我需要一个具有学说2的代码的具体示例,它使用"多态关联".让我澄清一下自己.我有一个名为Contract的实体,合约可以有很多价格规则,这些价格规则可以是不同类别的类,并且存在于不同的表中.我想这是多态关联,或者我错了?
class contract {
private $id;
private $priceRules;
}
class discountRule implements priceRule{
function calculate() {
// calculate new price after this rule
}
}
class extraSpecialRule implements priceRule {
function calculate() {
// calculate new price after this rule
}
}
Run Code Online (Sandbox Code Playgroud)
将来可能会有新类型的价格规则,那么我如何将这些规则与主要实体相关联并将它们固定在单独的表中呢?
更新:
这是我的新代码:
contract.php
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity @Table(name="contract")
*/
class Contract {
/**
*
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @Column(type="integer")
*/
private $propertyId;
/**
*
* @Column(type="integer")
*/ …Run Code Online (Sandbox Code Playgroud) doctrine domain-driven-design polymorphic-associations doctrine-orm
我正在建立一个类似于Yelp(推荐引擎,虽然规模较小)的网站,因此系统中将有三个主要实体:用户,地方(包括商家)和活动.
现在我想知道的是如何为每种类型的实体存储照片,评论和"赞美"(类似于Facebook的"赞")等信息,以及它们可以应用于每个对象的信息(例如评论推荐,照片等).现在,我这样做的方式是每个ie的单个表
类型表示的照片(id,type,owner_id,is_main等等)
:1 =用户,2 =地点,3 =事件注释(id,object_type,object_id,user_id,content等等)
,其中object_type可以是一些不同的对象,如照片,推荐等Compliment(object_id,object_type,compliment_type,user_id)
其中object_type可以是一些不同的对象,如照片,推荐等Activity(id,source,source_type,source_id等等)
//for "activity feed"
,其中source_type是用户,地点或事件通知(id,收件人,发件人,activity_type,object_type,object_id等等)
,其中object_type和object_id将用于提供与通知对象的直接链接,例如用户的照片被称赞
但是在 阅读 SO上的一些帖子之后,我意识到我无法使用外键保持参照完整性,因为这需要1:1的关系,而我的source_id/object_id字段可以与多个表中的ID相关.所以我决定采用保持主实体的方法,然后将其分解为子集,即
User_Photo(photo_id,user_id)| Place_Photo(photo_id,place_id)| 等等...
Photo_Comment(comment_id,photo_id)| Recommendation_Comment(comment_id,rec_id)| 等等...
赞美(id,...)
//would need to add a surrogate key to Compliment table nowPhoto_Compliment(compliment_id,photo_id)| Comment_Compliment(compliment_id,comment_id)| 等等...
User_Activity(activity_id,user_id)| Place_Activity(activity_id,place_id)| 等等...
我以为我可以创建将每个子表连接到主表的视图,以获得我想要的结果.另外我认为它也适合我在Code Igniter中的对象模型.
我认为唯一可以离开的表是通知表,因为有很多对象类型(论坛帖子,照片,推荐等等),这个表只会保留一周的通知,所以任何参考完整性问题都不应该是'这是一个很大的问题(我认为).
我是否以明智的方式解决这个问题?我可能忽略的任何性能,可靠性或其他问题?
我能看到的唯一"问题"是我最终会得到很多表(因为现在我有大约72个,所以我想在添加额外内容之后我最终会得到一些不到90个表),就我所知,这不是问题.
非常感谢任何反馈.提前致谢.
编辑:为了清楚起见,我并不担心我最终还有10张桌子.据我所知,表的数量并不是太大的问题(一旦它们被使用)......除非你说200左右:/
mysql database-design comments photo polymorphic-associations
我有一堆或多个地址,它们通过多态关联属于各种其他资源
belongs_to :addressable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
和我的资源,可以说它是一家酒店
has_one :address, :as => :addressable
Run Code Online (Sandbox Code Playgroud)
我可以搜索我的地址数据没问题,例如在我的搜索字符串的 20 英里半径内找到每个地址
@addresses = Address.near(params[:search],20, :order => :distance).page params[:page]
Run Code Online (Sandbox Code Playgroud)
我真的很难加入资源,即酒店信息,我可以链接到详细信息页面,但我想在我的结果中显示酒店的名称。
我认为使用 :include 可能有帮助:
@addresses = Address.near(params[:search],20, :order => :distance, :include => :hotel).page params[:page]
Run Code Online (Sandbox Code Playgroud)
..但没有运气
非常感谢任何建议。
炖
ruby-on-rails geolocation polymorphic-associations ruby-on-rails-3
所以我知道这个问题已被问了很多次,但我的问题更进一步.
在为我的应用程序建模时,我有两种类型的用户,它们与用户模型具有多态关联.如:
class User < ActiveRecord::Base
belongs_to :profileable, :polymorphic => true
end
class User_Type_1 < ActiveRecord::Base
has_one :user, :as => :profileable
end
class User_Type_2 < ActiveRecord::Base
has_one :user, :as => :profileable
end
Run Code Online (Sandbox Code Playgroud)
我这样做的原因,而不是STI,是因为User_Type_1有4个字段,User_Type_2有20个字段,我不希望用户表有这么多字段(是的24-ish字段不是很多,但我'大多数时候,我宁愿没有20个字段空了)
我理解这是如何工作的,我的问题是我希望注册表单只用于注册类型的用户,User_Type_1但签名表单用于两者.(我将有一个应用程序的管理员端,将创建用户User_Type_2)
我知道我可以after_sign_in_path_for(resource)以AppicationController某种方式使用覆盖在登录时重定向到网站的右侧部分.例如:
def after_sign_in_path_for(resource)
case current_user.profileable_type
when "user_type_1"
return user_type_1_index_path
when "user_type_2"
return user_type_1_index_path
end
end
Run Code Online (Sandbox Code Playgroud)
所以我想我的问题是如何让表单与Devise一起工作,只允许注册类型User_Type_1然后在sign_up之后签名?
另外,如果我以错误的方式解决这个问题,那么正确的方法是什么?
我正在构建一个Rails应用程序来跟踪组成员之间的费用/债务,让我们说一个家庭.到目前为止,我有群组,用户和费用的模型 - 基础知识.现在我正试图弄清楚群组和用户之间的关联.例如,一个组可以拥有许多用户,并且用户可以拥有/属于许多组,因此我使用连接表设置了HABTM关联.但我很困惑,因为一个集团也可以有一个所有者,也是一个用户.这就是我现在所处的位置:
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
has_one :owner, :class_name => "User"
end
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end
Run Code Online (Sandbox Code Playgroud)
Group表上当前有一个owner_id字段,但是column users.group_id does not exist当我尝试执行任何涉及的操作时,我收到PostgreSQL错误group.owner.我很丢失 - 关于在同一模型中表示多个关联的最佳方式的任何想法?
我在Laravel 4中有多态关系,它的工作方式与我想要的一样.我有另一个,当我尝试插入相关模型时,它不起作用,即使它与第一个相同,我以相同的方式使用它.
我有Event模特和Article模特:
// Article Model
class Article extends Eloquent {
public function events() {
return $this->morphMany('Event', 'eventable');
}
...
// Event Model
class Event extends Eloquent {
public function eventable() {
return $this->morphTo();
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
$article = Article::find($article_id);
// insert
// neither of the following lines work
$article->events()->create(array("type" => "click"));
$article->events()->save(new Event(array("type" => "click")));
Run Code Online (Sandbox Code Playgroud)
我犯了同样的错误:
Error: Call to undefined method Illuminate\Support\Facades\Event::newQuery() in
\app_path\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php line 383
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我有一个相同的情况与文章和它的照片,但它有效(它也是一个多态关系).
我是一个铁杆新手,正在努力理解在具有相关外键的多个belongs_to声明中使用多态关联的优势.例如,在Ryan Bates的railscast(http://railscasts.com/episodes/154-polymorphic-association-revised)中,文章,事件和照片都可以有很多注释,因此他使用可评论来建立多态关联.
为什么不只是将注释属于其他三个资产中的每一个,并在其表中包含article_id,event_id和photo_id外键,其中只有一个非空?
我的应用程序中有以下模型
class User < ActiveRecord::Base
has_many :articles
has_many :tour_companies
has_many :accomodations
end
class Articles < ActiveRecord::Base
belongs_to :user
belongs_to :bloggable, :polymorphic => true
end
class TourCompany < ActiveRecord::Base
belongs_to :user
has_many :articles, :as => :bloggable
end
class Accommodation < ActiveRecord::Base
belongs_to :user
has_many :articles, :as => :bloggable
end
Run Code Online (Sandbox Code Playgroud)
现在我的问题是我想要一个登录用户能够写一篇文章并使用表单collection_select来选择他/她的旅游公司或文章应该与之相关的住宿,我如何在rails 4中做到这一点?如何从表单集选择中选择bloggable类型和id?我不想要嵌套资源
我的Rails应用程序中的模型很少,它们是:
我需要评论belog要么到Photo或Album,显然永远属于User.我将使用多态关联.
# models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :commentable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
问题是,Rails描述#create新评论行动的方式是什么.我看到两个选项.
1.描述每个控制器中的注释创建
但这不是一个干燥的解决方案.我可以为显示和创建注释创建一个常见的局部视图,但我将不得不重复自己为每个控制器编写注释逻辑.所以它不起作用
2.创建新的CommentsController
这是我猜的正确方法,但我知道:
要使其工作,您需要声明模型中的外键列和类型列,以声明多态接口
像这样:
# schema.rb
create_table "comments", force: :cascade do |t|
t.text "body"
t.integer "user_id"
t.integer "commentable_id"
t.string "commentable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Run Code Online (Sandbox Code Playgroud)
所以,当我编写非常简单的控制器时,它将接受来自远程表单的请求:
# controllers/comments_controller.rb
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@commentable = ??? …Run Code Online (Sandbox Code Playgroud) ruby activerecord ruby-on-rails polymorphic-associations ruby-on-rails-4
activerecord ×2
comments ×1
devise ×1
doctrine ×1
doctrine-orm ×1
foreign-keys ×1
geolocation ×1
laravel ×1
laravel-4 ×1
mysql ×1
photo ×1
polymorphism ×1
ruby ×1
sql ×1