feat(dev): Add an example for transient db entity attributes

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst
2024-03-27 10:03:35 +01:00
parent 776044f7d7
commit 864617c80f

View File

@@ -248,6 +248,8 @@ The following types can be added for a field:
* ``json`` - JSON data is automatically decoded on reading
* ``datetime`` - Providing ``\DateTime()`` objects
.. _database-entity-attribute-access:
Accessing attributes
^^^^^^^^^^^^^^^^^^^^
@@ -317,6 +319,35 @@ mapping, simply override the **columnToProperty** and **propertyToColumn** metho
.. _database-entity-slugs:
Transient attributes
^^^^^^^^^^^^^^^^^^^^
You can add attributes to an entity class that do not map to a database column. These are called *transient* because they are neither loaded from database rows nor are their values persisted.
.. code-block:: php
:caption: lib/Db/User.php
<?php
namespace OCA\MyApp\Db;
use OCP\AppFramework\Db\Entity;
class User extends Entity {
protected string $uid; // Exists in the database
protected $lastLogin; // Does not exist in the database
public function getLastLogin(): ?int {
return $this->lastLogin;
}
public function setLastLogin(int $lastLogin): void {
$this->lastLogin = $lastLogin;
}
}
It is important to define getters and setters for any transient attributes. Do not use the :ref:`magic getters and setters<database-entity-attribute-access>` of attributes that map to database columns.
Slugs
^^^^^