doc/book/en/development/entityclasses/data-as-objects.rst
branchstable
changeset 3283 4f53eb3f1331
parent 2476 1294a6bdf3bf
child 4437 21f2e01fdd6a
equal deleted inserted replaced
3258:6536ee4f37f7 3283:4f53eb3f1331
     1 Access to persistent data
     1 Access to persistent data
     2 --------------------------
     2 --------------------------
     3 
     3 
     4 XXX is provided by the :class:`Entity <cubicweb.entity.entity>` class
     4 Python-level access to persistent data is provided by the
       
     5 :class:`Entity <cubicweb.entity>` class.
     5 
     6 
     6 An entity class is bound to a schema entity type.  Descriptors are added when
     7 An entity class is bound to a schema entity type.  Descriptors are added when
     7 classes are registered in order to initialize the class according to its schema:
     8 classes are registered in order to initialize the class according to its schema:
     8 
     9 
     9 * we can access the defined attributes in the schema thanks to the attributes of
    10 * we can access the defined attributes in the schema thanks to the attributes of
    20   * `absolute_url(**kwargs)`, returns an absolute URL to access the primary view
    21   * `absolute_url(**kwargs)`, returns an absolute URL to access the primary view
    21     of an entity
    22     of an entity
    22 
    23 
    23   * `rest_path()`, returns a relative REST URL to get the entity
    24   * `rest_path()`, returns a relative REST URL to get the entity
    24 
    25 
    25   * `format(attr)`, returns the format (MIME type) of the field given un parameter
       
    26 
       
    27   * `printable_value(attr, value=_marker, attrtype=None, format='text/html')`,
    26   * `printable_value(attr, value=_marker, attrtype=None, format='text/html')`,
    28     returns a string enabling the display of an attribute value in a given format
    27     returns a string enabling the display of an attribute value in a given format
    29     (the value is automatically recovered if necessary)
    28     (the value is automatically recovered if necessary)
    30 
    29 
    31 :Data handling:
    30 :Data handling:
    32 
    31 
    33   * `as_rset()`, converts the entity into an equivalent result set simulating the
    32   * `as_rset()`, converts the entity into an equivalent result set simulating the
    34      request `Any X WHERE X eid _eid_`
    33      request `Any X WHERE X eid _eid_`
    35 
    34 
    36   * `complete(skip_bytes=True)`, executes a request that recovers in one time
    35   * `complete(skip_bytes=True)`, executes a request that recovers all at once
    37     all the missing attributes of an entity
    36     all the missing attributes of an entity
    38 
    37 
    39   * `get_value(name)`, returns the value associated to the attribute name given
    38   * `get_value(name)`, returns the value associated to the attribute name given
    40     in parameter
    39     in parameter
    41 
    40 
    50     values given named parameters
    49     values given named parameters
    51 
    50 
    52   * `copy_relations(ceid)`, copies the relations of the entities having the eid
    51   * `copy_relations(ceid)`, copies the relations of the entities having the eid
    53     given in the parameters on the current entity
    52     given in the parameters on the current entity
    54 
    53 
    55   * `last_modified(view)`, returns the date the object has been modified
       
    56     (used by HTTP cache handling)
       
    57 
       
    58   * `delete()` allows to delete the entity
    54   * `delete()` allows to delete the entity
    59 
    55 
    60 
    56 
    61 Tne :class:`AnyEntity` class
    57 Tne :class:`AnyEntity` class
    62 ----------------------------
    58 ----------------------------
    64 To provide a specific behavior for each entity, we have to define a class
    60 To provide a specific behavior for each entity, we have to define a class
    65 inheriting from `cubicweb.entities.AnyEntity`. In general, we define this class
    61 inheriting from `cubicweb.entities.AnyEntity`. In general, we define this class
    66 in `mycube.entities` module (or in a submodule if we want to split code among
    62 in `mycube.entities` module (or in a submodule if we want to split code among
    67 multiple files) so that it will be available on both server and client side.
    63 multiple files) so that it will be available on both server and client side.
    68 
    64 
    69 The class `AnyEntity` is loaded dynamically from the class `Entity`
    65 The class `AnyEntity` is a sub-class of Entity that add methods to it,
    70 (`cubciweb.entity`). We define a sub-class to add methods or to
    66 and helps specializing (by further subclassing) the handling of a
    71 specialize the handling of a given entity type
    67 given entity type.
    72 
    68 
    73 The methods defined for `AnyEntity` or `Entity` are the following ones:
    69 The methods defined for `AnyEntity`, in addition to `Entity`, are the
       
    70 following ones:
    74 
    71 
    75 :Standard meta-data (Dublin Core):
    72 :Standard meta-data (Dublin Core):
    76 
    73 
    77   * `dc_title()`, returns a unicode string corresponding to the meta-data
    74   * `dc_title()`, returns a unicode string corresponding to the
    78     `Title` (used by default the first attribute non-meta of the entity
    75     meta-data `Title` (used by default is the first non-meta attribute
    79     schema)
    76     of the entity schema)
    80 
    77 
    81   * `dc_long_title()`, same as dc_title but can return a more
    78   * `dc_long_title()`, same as dc_title but can return a more
    82     detailled title
    79     detailed title
    83 
    80 
    84   * `dc_description(format='text/plain')`, returns a unicode string
    81   * `dc_description(format='text/plain')`, returns a unicode string
    85     corresponding to the meta-data `Description` (look for a description
    82     corresponding to the meta-data `Description` (looks for a
    86     attribute by default)
    83     description attribute by default)
    87 
    84 
    88   * `dc_authors()`, returns a unicode string corresponding to the meta-data
    85   * `dc_authors()`, returns a unicode string corresponding to the meta-data
    89     `Authors` (owners by default)
    86     `Authors` (owners by default)
    90 
    87 
    91   * `dc_date(date_format=None)`, returns a unicode string corresponding to
    88   * `dc_date(date_format=None)`, returns a unicode string corresponding to