2 |
2 |
3 |
3 |
4 Data as objects |
4 Data as objects |
5 =============== |
5 =============== |
6 |
6 |
7 We will in this chapter introduce the objects that are used to handle |
7 In this chapter, we will introduce the objects that are used to handle |
8 the data stored in the database. |
8 the data stored in the database. |
9 |
9 |
10 Classes `Entity` and `AnyEntity` |
10 Classes `Entity` and `AnyEntity` |
11 -------------------------------- |
11 -------------------------------- |
12 |
12 |
13 To provide a specific behavior for each entity, we just need to define |
13 To provide a specific behavior for each entity, we have to define |
14 a class inheriting from `cubicweb.entities.AnyEntity`. In general, we have |
14 a class inheriting from `cubicweb.entities.AnyEntity`. In general, we |
15 to defined those classes in a module of `entities` package of an application |
15 define this class in a module of `entities` package of an application |
16 so that it will be available on both server and client side. |
16 so that it will be available on both server and client side. |
17 |
17 |
18 The class `AnyEntity` is loaded dynamically from the class `Entity` |
18 The class `AnyEntity` is loaded dynamically from the class `Entity` |
19 (`cubciweb.common.entity`). We define a sub-class to add methods or to |
19 (`cubciweb.common.entity`). We define a sub-class to add methods or to |
20 specialize the handling of a given entity type |
20 specialize the handling of a given entity type |
21 |
21 |
22 Descriptors are added when classes are registered in order to initialize the class |
22 Descriptors are added when classes are registered in order to initialize the class |
23 according to its schema: |
23 according to its schema: |
24 |
24 |
25 * we can access the defined attributes in the schema thanks the attributes of |
25 * we can access the defined attributes in the schema thanks to the attributes of |
26 the same name on instances (typed value) |
26 the same name on instances (typed value) |
27 |
27 |
28 * we can access the defined relations in the schema thanks to the relations of |
28 * we can access the defined relations in the schema thanks to the relations of |
29 the same name on instances (entities instances list) |
29 the same name on instances (entities instances list) |
30 |
30 |
31 The methods defined for `AnyEntity` or `Entity` are the following ones: |
31 The methods defined for `AnyEntity` or `Entity` are the following ones: |
32 |
32 |
33 * `has_eid()`, returns true is the entity has an definitive eid (e.g. not in the |
33 * `has_eid()`, returns true is the entity has an definitive eid (e.g. not in the |
34 creation process) |
34 creation process) |
35 |
35 |
36 * `check_perm(action)`, checks if the user has the permission to execcute the |
36 * `check_perm(action)`, checks if the user has the permission to execute the |
37 requested action on the entity |
37 requested action on the entity |
38 |
38 |
39 :Formatting and output generation: |
39 :Formatting and output generation: |
40 |
40 |
41 * `view(vid, **kwargs)`, apply the given view to the entity |
41 * `view(vid, **kwargs)`, applies the given view to the entity |
42 |
42 |
43 * `absolute_url(**kwargs)`, returns an absolute URL to access the primary view |
43 * `absolute_url(**kwargs)`, returns an absolute URL to access the primary view |
44 of an entity |
44 of an entity |
45 |
45 |
46 * `rest_path()`, returns a relative REST URL to get the entity |
46 * `rest_path()`, returns a relative REST URL to get the entity |
116 |
116 |
117 |
117 |
118 *rtags* |
118 *rtags* |
119 ------- |
119 ------- |
120 |
120 |
121 *rtags* allows to specify certain behaviors of relations relative to a given |
121 *rtags* allow to specify certain behaviors of relations relative to a given |
122 entity type (see later). They are defined on the entity class by the attribute |
122 entity type (see later). They are defined on the entity class by the attribute |
123 `rtags` which is a dictionnary with as its keys the triplet :: |
123 `rtags` which is a dictionnary with as keys the triplets :: |
124 |
124 |
125 <relation type>, <target entity type>, <context position ("subject" ou "object")> |
125 <relation type>, <target entity type>, <context position ("subject" ou "object")> |
126 |
126 |
127 and as the values a `set` or a tuple of markers defining the properties that |
127 and as values a `set` or a tuple of markers defining the properties that |
128 apply to this relation. |
128 apply to this relation. |
129 |
129 |
130 It is possible to simplify this dictionnary: |
130 It is possible to simplify this dictionnary: |
131 |
131 |
132 * if we want to specifiy a single marker, it is not necessary to |
132 * if we want to specifiy a single marker, it is not necessary to |
133 use a tuple as the value, the marker by itself (characters string) |
133 use a tuple as value, the marker by itself (character string) |
134 is enough |
134 is enough |
135 * if we only care about a single type of relation and not about the target |
135 * if we only care about a single type of relation and not about the target |
136 and the context position (or when this one is not ambigous), we can simply |
136 and the context position (or when this one is not ambigous), we can simply |
137 use the name of the relation type as the key |
137 use the name of the relation type as key |
138 * if we want a marker to apply independently from the target entity type, |
138 * if we want a marker to apply independently from the target entity type, |
139 we have to use the string `*` as the target entity type |
139 we have to use the string `*` as target entity type |
140 |
140 |
141 |
141 |
142 Please note that this dictionnary is *treated at the time the class is created*. |
142 Please note that this dictionnary is *treated at the time the class is created*. |
143 It is automatically merged with the parent class(es) (no need to copy the |
143 It is automatically merged with the parent class(es) (no need to copy the |
144 dictionnary from the parent class to modify it). Also, modify it after the |
144 dictionnary from the parent class to modify it). Also, modifying it after the |
145 class is created will not have any effect... |
145 class is created will not have any effect... |
146 |
146 |
147 .. include:: B0031-define-entities.en.txt |
147 .. include:: B0031-define-entities.en.txt |
|
148 |