Laravel:使用Eloquent ORM的N到N(mysql)关系(Laravel: N to N (mysql) relation using Eloquent ORM)
我有4张桌子:
// Table countries +----+------+ | Id | Name | +----+------+ | 1 | USA | | 2 | GB | +----+------+ // Table platforms +----+---------+ | Id | Name | +----+---------+ | 1 | Windows | | 2 | Linux | +----+---------+ // Table users +----+-------+------------+-------------+ | Id | Name | country_id | platform_id | +----+-------+------------+-------------+ | 1 | Admin | 1 | 1 | | 2 | Test | 2 | 1 | +----+-------+------------+-------------+ // Table posts +----+-----------+------------+-------------+---------+ | Id | Title | country_id | platform_id | user_id | +----+-----------+------------+-------------+---------+ | 1 | TestPost1 | 2 | 1 | 1 | | 2 | TestPost2 | 2 | 2 | null | +----+-----------+------------+-------------+---------+
数据库应该能够实现以下关系:
- 用户(N)< - >(N)平台
- 用户(N)< - >(N)国家
- 用户(0..1)< - >(N)发布
- 邮政(N)< - >(N)国家
- 发布(N)< - >(1)平台
所以现在我尝试在Laravel Eloquent ORM文档之后实现这些关系:
// Country.php public function posts() { return $this->belongsToMany('App\Post'); } public function users() { return $this->belongsToMany('App\User'); } // Platform.php public function users() { return $this->belongsToMany('App\User'); } public function posts() { return $this->belongsToMany('App\Post'); } // User.php public function posts() { return $this->hasMany('App\Post'); } public function countries() { return $this->hasMany('App\Country'); } public function platforms() { return $this->hasMany('App\Platform'); } // Post.php public function countries() { return $this->hasMany('App\Country'); } public function platforms() { return $this->hasMany('App\Comment'); } public function user() { return $this->belongsTo('App\User', 'user_id'); }
但现在我很困惑,因为我认为在mysql中实现N到N关系的方法是向db添加第三个表,例如:
// Table CountryUserRelations to implement User (N) <-> (N) Country +----+------------+---------+ | Id | country_id | user_id | +----+------------+---------+ | 1 | 1 | 1 | | 2 | 2 | 1 | | 3 | 1 | 2 | | 4 | 2 | 2 | +----+------------+---------+
但是Eloquent ORM如何处理我模型中的规则? 它是否会保持N到N的关系而不必添加关系表? 或者我错过了什么或误解了雄辩的ORM关系概念?
I got 4 tables:
// Table countries +----+------+ | Id | Name | +----+------+ | 1 | USA | | 2 | GB | +----+------+ // Table platforms +----+---------+ | Id | Name | +----+---------+ | 1 | Windows | | 2 | Linux | +----+---------+ // Table users +----+-------+------------+-------------+ | Id | Name | country_id | platform_id | +----+-------+------------+-------------+ | 1 | Admin | 1 | 1 | | 2 | Test | 2 | 1 | +----+-------+------------+-------------+ // Table posts +----+-----------+------------+-------------+---------+ | Id | Title | country_id | platform_id | user_id | +----+-----------+------------+-------------+---------+ | 1 | TestPost1 | 2 | 1 | 1 | | 2 | TestPost2 | 2 | 2 | null | +----+-----------+------------+-------------+---------+
The database should be able to implement the following relations:
- User (N) <-> (N) Platform
- User (N) <-> (N) Country
- User (0..1) <-> (N) Post
- Post (N) <-> (N) Country
- Post (N) <-> (1) Platform
So now I tried to implement these relations following Laravel Eloquent ORM documentation:
// Country.php public function posts() { return $this->belongsToMany('App\Post'); } public function users() { return $this->belongsToMany('App\User'); } // Platform.php public function users() { return $this->belongsToMany('App\User'); } public function posts() { return $this->belongsToMany('App\Post'); } // User.php public function posts() { return $this->hasMany('App\Post'); } public function countries() { return $this->hasMany('App\Country'); } public function platforms() { return $this->hasMany('App\Platform'); } // Post.php public function countries() { return $this->hasMany('App\Country'); } public function platforms() { return $this->hasMany('App\Comment'); } public function user() { return $this->belongsTo('App\User', 'user_id'); }
But now I am confused, as I thought the way to implement N to N relations in mysql is to add a third table to db, for example like that:
// Table CountryUserRelations to implement User (N) <-> (N) Country +----+------------+---------+ | Id | country_id | user_id | +----+------------+---------+ | 1 | 1 | 1 | | 2 | 2 | 1 | | 3 | 1 | 2 | | 4 | 2 | 2 | +----+------------+---------+
But how does Eloquent ORM handle the rules inside my model? Will it keep the N to N relations without having to add a relations table? Or am I missing something or misunderstanding the Eloquent ORM Relations concept?
原文:https://stackoverflow.com/questions/43416437
最满意答案
这是因为你将
TintColor
的alpha
设置为0
。 它等于不可见。应用于按钮标题和图像的色调颜色。
所以你需要修改第3行的xib,如下所示
<color key="tintColor" red="0.0" green="0.0" blue="0.0" alpha="1.0" colorSpace="custom" customColorSpace="sRGB"/>
It is because of that you set the
alpha
ofTintColor
to0
. It equals to invisible.The tint color to apply to the button title and image.
So you need to modify the xib at line 3 as below
<color key="tintColor" red="0.0" green="0.0" blue="0.0" alpha="1.0" colorSpace="custom" customColorSpace="sRGB"/>
相关问答
更多-
您可以设置按钮的Padding You can set the Button's Padding
-
这是Xamarin.Forms中的默认行为。 要解决该问题并实现自定义颜色,您可以: 使用触发器在从启用到禁用时更改颜色,反之亦然 在启用和禁用按钮时手动设置颜色 使用自定义渲染器 因为触发器是实现你想要的最直接,最一致的方式,所以我只会介绍它。 您可以看到xamarin 文档以获取更多关于自定义渲染器的信息。 使用触发器时,可以在未启用时动态地将字体颜色更改为灰色。 有关触发器的更多信息,请参阅文档 。
-
嘿Namratha,如果你要求改变UIButton的文本和启用/禁用状态,它可以很容易地完成如下; [myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title [myButton setEnabled:NO]; // To toggle enabled / disabled 如果您已经在Interface Builder中创建了按钮并希望以代码方式访问它们,则可以利用它们作为参数传 ...
-
iOS - 无法在按钮上设置文字(iOS - can't set text on button)[2023-04-29]
这是因为你将TintColor的alpha设置为0 。 它等于不可见。 什么是按钮上的tintColor 应用于按钮标题和图像的色调颜色。 所以你需要修改第3行的xib,如下所示It is because of that you set the alpha of TintColo ... -
这是因为字体不会神奇地使自己大胆。 粗体字体是分开设计的。 恐怕这个问题没有答案,除了增加线条的中风,这可能会违反你的字体协议。 That's because fonts don't magically make themselves bold. Bold fonts are designed separately. I'm afraid there is no answer to this question, apart from thickening the stroke of the lines, w ...
-
关于什么 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(5, 0) forBarMetrics:UIBarMetricsDefault]; What about [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(5, 0) forBarMetrics:UIBarMetricsDefau ...
-
后面的标题取决于ViewController的Title属性。 它可以如下设置,这里vc是在UINavigationController内部推入的控制器的实例。 vc.NavigationItem.SetLeftBarButtonItem (new UIBarButtonItem( UIImage.FromFile("29_icon.png"), UIBarButtonItemStyle.Plain, (sender, args) => { NavigationController.PopViewC ...
-
设计iOS按钮可扩展(Design iOS button scalable)[2022-06-20]
这是关于约束。 在设备上使其变得更大/更小时,您需要将按钮的边缘与视图的边缘相连:单击Storyboard工作区下方看起来像Tie-Fighter的图标。 要在文本和按钮边缘之间获得间距,请使用内容插入 - 位于尺寸检查器面板上(右侧有一个图标看起来像垂直标尺的那个)。实际上,您应该将自动布局和尺寸约束读为构建UI将需要对此材料有很好的了解。 This is all about Constraints. In terms of making it bigger/smaller on devices you ... -
无法将按钮文字设定为“<<<”(Cannot set button text to “<<<”)[2023-09-20]
这些是html实体,也适用于android studio。 > can be replaced with > < can be replaced with < " can be replaced with ", “ or ” ' can be replaced with ', ‘ or ’ } can be replaced with } & can be replaced with & These are ... -
文字输入ios问题(Text entry ios question)[2022-03-06]
您将要做的是用文本字段替换标签,并在用户按下按钮时将其设置为第一响应者。 您将需要一个作为文本字段的委托的类,以便您处理输入textFieldShouldReturn方法以在文本字段上重新签名第一个响应者(以关闭键盘)并将文本字段更改回标签视图并更新其内容。 What you are going to have to do is replace the label with a text field and set that as the first responder when the user pres ...