mirror of
https://github.com/nextcloud/documentation.git
synced 2026-01-02 17:59:36 +07:00
feat(dev): Add an example for transient db entity attributes
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
@@ -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
|
||||
^^^^^
|
||||
|
||||
|
||||
Reference in New Issue
Block a user