many to many docs
This commit is contained in:
parent
cbc6c4bb31
commit
d1d3b05ac3
1 changed files with 17 additions and 33 deletions
50
README.md
50
README.md
|
@ -175,19 +175,11 @@ Relationships use joins when possible, but in the case of `@HasMany()`, two quer
|
||||||
|
|
||||||
## Many to Many Relations
|
## Many to Many Relations
|
||||||
A many-to-many relationship can now be modeled like so.
|
A many-to-many relationship can now be modeled like so.
|
||||||
`UserRole` in this case is a pivot table joining `User` and `Role`.
|
`RoleUser` in this case is a pivot table joining `User` and `Role`.
|
||||||
|
|
||||||
Note that in this case, the models must reference the private classes (`_User`, etc.), because the canonical versions (`User`, etc.) are not-yet-generated:
|
Note that in this case, the models must reference the private classes (`_User`, etc.), because the canonical versions (`User`, etc.) are not-yet-generated:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
library angel_orm_generator.test.models.user;
|
|
||||||
|
|
||||||
import 'package:angel_model/angel_model.dart';
|
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
|
||||||
import 'package:angel_serialize/angel_serialize.dart';
|
|
||||||
import 'package:collection/collection.dart';
|
|
||||||
part 'user.g.dart';
|
|
||||||
|
|
||||||
@serializable
|
@serializable
|
||||||
@orm
|
@orm
|
||||||
abstract class _User extends Model {
|
abstract class _User extends Model {
|
||||||
|
@ -195,10 +187,18 @@ abstract class _User extends Model {
|
||||||
String get password;
|
String get password;
|
||||||
String get email;
|
String get email;
|
||||||
|
|
||||||
@hasMany
|
@ManyToMany(_RoleUser)
|
||||||
List<_UserRole> get userRoles;
|
List<_Role> get roles;
|
||||||
|
}
|
||||||
|
|
||||||
List<_Role> get roles => userRoles.map((m) => m.role).toList();
|
@serializable
|
||||||
|
@orm
|
||||||
|
abstract class _RoleUser {
|
||||||
|
@belongsTo
|
||||||
|
_Role get role;
|
||||||
|
|
||||||
|
@belongsTo
|
||||||
|
_User get user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@serializable
|
@serializable
|
||||||
|
@ -206,35 +206,19 @@ abstract class _User extends Model {
|
||||||
abstract class _Role extends Model {
|
abstract class _Role extends Model {
|
||||||
String name;
|
String name;
|
||||||
|
|
||||||
@hasMany
|
@ManyToMany(_RoleUser)
|
||||||
List<_UserRole> get userRoles;
|
List<_User> get users;
|
||||||
|
|
||||||
List<_User> get users => userRoles.map((m) => m.user).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable(autoIdAndDateFields: false)
|
|
||||||
@orm
|
|
||||||
abstract class _UserRole {
|
|
||||||
int get id;
|
|
||||||
|
|
||||||
@belongsTo
|
|
||||||
_User get user;
|
|
||||||
|
|
||||||
@belongsTo
|
|
||||||
_Role get role;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
TLDR:
|
TLDR:
|
||||||
1. Make a pivot table, C, between two tables, table A and B
|
1. Make a pivot table, C, between two tables, table A and B
|
||||||
2. C should `@belongsTo` both A and B.
|
2. C should `@belongsTo` both A and B. C *should not* extend `Model`.
|
||||||
3. Both A and B should `@hasMany` C.
|
3. A should have a field: `@ManyToMany(_C) List<_B> get b;`
|
||||||
4. For convenience, write a simple getter, like the above `User.roles`.
|
4. B should have a field: `@ManyToMany(_C) List<_A> get a;`
|
||||||
|
|
||||||
Test: https://raw.githubusercontent.com/angel-dart/orm/master/angel_orm_generator/test/many_to_many_test.dart
|
Test: https://raw.githubusercontent.com/angel-dart/orm/master/angel_orm_generator/test/many_to_many_test.dart
|
||||||
|
|
||||||
There are 2 tests there, but they are more or less a proof-of-concept. All the logic for the other relations have their own unit tests.
|
|
||||||
|
|
||||||
# Columns
|
# Columns
|
||||||
Use a `@Column()` annotation to change how a given field is handled within the ORM.
|
Use a `@Column()` annotation to change how a given field is handled within the ORM.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue