if you want to automatically generate the models, consult the generating models part.
A model class is just a plain old php object without inheritance.
Models are located by default in the app\models folder.
Object Relational Mapping (ORM) relies on member annotations or attributes (since PHP8) in the model class.
A basic model¶
- A model must define its primary key using the @id annotation on the members concerned
- Serialized members must have getters and setters
- Without any other annotation, a class corresponds to a table with the same name in the database, each member corresponds to a field of this table
If the name of the table is different from the name of the class, the annotation @table allows to specify the name of the table.
If the name of a field is different from the name of a member in the class, the annotation @column allows to specify a different field name.
Foreign key field names consist of the primary key name of the referenced table followed by the name of the referenced table whose first letter is capitalized.
idUser for the table
user whose primary key is
A user belongs to an organization:
The @joinColumn annotation or the JoinColumn attribute specifies that:
- The member $organization is an instance of modelsOrganization
- The table user has a foreign key idOrganization refering to organization primary key
- This foreign key is not null => a user will always have an organization
An organization has many users:
In this case, the association is bi-directional.
The @oneToMany annotation must just specify:
- The class of each user in users array : modelsUser
- the value of @mappedBy is the name of the association-mapping attribute on the owning side : $organization in User class
- A user can belong to groups.
- A group consists of multiple users.
If the naming conventions are not respected for foreign keys,
it is possible to specify the related fields.
Annotations for classes¶
|@database||Defines the associated database offset (defined in config file)|
|@table||Defines the associated table name.|
Annotations for members¶
|@id||Defines the primary key(s).|
|@column||Specify the associated field characteristics.||name||Name of the associated field|
|nullable||true if value can be null|
|dbType||Type of the field in database|
|@transient||Specify that the field is not persistent.|
|@annotation (extends)||role||properties [optional]||role|
|@manyToOne||Defines a single-valued association to another entity class.|
|@joinColumn (@column)||Indicates the foreign key in manyToOne asso.||className||Class of the member|
|[referencedColumnName]||Name of the associated column|
|@oneToMany||Defines a multi-valued association to another entity class.||className||Class of the objects in member|
|[mappedBy]||Name of the association-mapping attribute on the owning side|
|@manyToMany||Defines a many-valued association with many-to-many multiplicity||targetEntity||Class of the objects in member|
|[inversedBy]||Name of the association-member on the inverse-side|
|[mappedBy]||Name of the association-member on the owning side|
|@joinTable||Defines the association table for many-to-many multiplicity||name||The name of the association table|
|[joinColumns]||@column => name and referencedColumnName for this side|
|[inverseJoinColumns]||@column => name and referencedColumnName for the other side|