Doctrine ORM

PHPMaker supports [Doctrine ORM](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/index.html). If you have not used Doctrine ORM before, you may want to read [Getting Started with Doctrine](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/tutorials/getting-started.html) first. Doctrine ORM is an [object-relational mapper (ORM)](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) for PHP that provides transparent persistence for PHP objects. Every PHP object that you want to save in the database using Doctrine is called an **Entity**. An entity is identified by a unique identifier or primary key and it contains persistable properties. A persistable property is an instance variable of the entity that is saved into and retrieved from the database by Doctrine's data mapping capabilities. PHPMaker generates entities for your project by [Basic Mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/basic-mapping.html#basic-mapping) and uses the [Attributes](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/attributes-reference.html) approach to map entity properties to columns in the table. You can read [Property Mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/basic-mapping.html#property-mapping) for details.
**Notes** 1. For binary field with "FILE" Edit Tag (see Field Setup), data is converted to stream type. If you need to convert it to string, you can use [stream_get_contents()](https://www.php.net/manual/en/function.stream-get-contents.php). 1. Date/Time field value is converted to PHP [DateTime](https://www.php.net/manual/en/class.datetime.php) type, not string. 1. See [Mapping Matrix](https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/types.html#mapping-matrix) for details.
To manage the persistence of your objects and to query for persistent objects, you need to use the ``EntityManager`` class which is a central access point to the functionality provided by Doctrine ORM. The global ``EntityManager()`` function is a PHPMaker function to get the **EntityManager** for database(s), see [Global Functions](functions.html) for details. #### Entities and the Identity Map Entities are objects with identity. Doctrine keeps a map of each entity and ids that have been retrieved per PHP request and keeps returning you the same instances. For example, you can find a user with the ID (primary key) 1234 by: Note that the entities are generated under the project's "Entity" namespace, you can get the full class name by ``Entity\Foobar::class`` where "Foobar" is the entity class name of a table. The entity class name is basically the **singular form of the table name in Pascal case**, e.g. if the table name is "Users", then the entity class name is ``User``. If the table name cannot be singularized, the original table name will be used. If the table's entity name is a PHP keyword, the entity will be prefixed with an underscore, e.g. if the table is "Classes", then the entity class name is ``_Class``. To check the entity names of a table, see [Database, Table and Field Variable Names](tools.html?id=vars). #### Persisting entities An entity can be made persistent by passing it to the ``EntityManager#persist($entity)`` method. By applying the persist operation on some entity, that entity becomes MANAGED, which means that its persistence is from now on managed by an **EntityManager**. As a result the persistent state of such an entity will subsequently be properly synchronized with the database when ``EntityManager#flush()`` is invoked. To get/set a field value, you use the ``getFooBar()`` and ``setFooBar()`` methods where "Foobar" is the variable name of the field. For example, if the field name is "Name", then getter/setter names will be ``getName()`` and ``setName()``. To check the getter/setter names of a table, see [Database, Table and Field Variable Names](tools.html?id=vars). Note that PHP method names are case-insensitive, so the getter/setter names are case-insensitive, but as a best practice you should write method names in camel case. #### Removing entities An entity can be removed from persistent storage by passing it to the ``EntityManager#remove($entity)`` method. By applying the remove operation on some entity, that entity becomes REMOVED, which means that its persistent state will be deleted once ``EntityManager#flush()`` is invoked. ##### SoftDeleteable If you have enabled the [Soft delete field](tablesetup.html?id=soft-delete-field), you can "soft delete" objects, filtering them at SELECT time by marking them deleted as with a timestamp, but not explicitly removing them from the database. For example, if you have a table named "Articles", then the entity class name is ``Article``, and you have set a field named "deletedAt" as the **Soft delete field**: #### Querying Doctrine ORM provides the following ways, in increasing level of power and flexibility, to query for persistent objects. You should always start with the simplest one that suits your needs. ##### By Primary Key The most basic way to query for a persistent object is by its ID (primary key) using the ``EntityManager#find($entityName, $id)`` method. Here is an example: The return value is either the found entity instance or null if no instance could be found with the given ID. Essentially, ``EntityManager#find()`` is just a shortcut for the following: ``EntityManager#getRepository($entityName)`` returns a repository object which provides many ways to retrieve entities of the specified type. By default, the repository instance is of type ``Doctrine\ORM\EntityRepository``. ##### By Simple Conditions To query for one or more entities based on several conditions that form a logical conjunction, use the ``findBy()`` and ``findOneBy()`` methods on a repository as follows: The ``EntityRepository#findBy()`` method additionally accepts orderings, limit and offset as second to fourth parameters: If you pass an array of values Doctrine will convert the query into a ``WHERE field IN (..)`` query automatically:

 

**Notes** 1. Entities works with the **Field Encryption** and **RemoveXSS** [extensions](extension.html). 1. Entities do NOT use any server events (e.g. **Row_XXX** server events). 1. Entities cannot be serialized directly by ``json_encode()``, but you can easily convert an entity to an associative array by the ``toArray()`` method of the entity.

 

Also See

[Global Functions](functions.html)
[Basic Mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/basic-mapping.html#basic-mapping)
[Working with Objects](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/working-with-objects.html#working-with-objects)

 

 ©2002-2025 e.World Technology Ltd. All rights reserved.