-
Notifications
You must be signed in to change notification settings - Fork 0
Defining the Entities
In this chapter you will learn
- how to configure your entities to map them to the database.
- how to define relationships.
Important: Be sure, that your database tables correspond to your entities. Mind that datatypes are correct. Set indices and foreign keys. PPA
will not do that for you.
You can annotate a class with @table(name = '<tablename>')
. So the class will be mapped to the database table <tablename>
. If the parameter name
is not defined, than the class will be mapped to its short name. Don't forget, that the resulting tablename is always assumed to be lowercase.
Note: It is not necessary to define the @table
annotation anyway. The only requirement, a class must fullfill, is to extend the class \PPA\core\Entity
.
Example:
/** @table(name = "user") */
class User extends \PPA\core\Entity { }
You can annotate class properties with a few annotations. To map a class property to a column it is necessary to make the @column
annotation. PPA
will not interfere in any other way to your class definitions. It does not even matter what scope your properties have.
Example:
/** @column(name = "username") */
private $username;
Relations are the powerful feature of PPA
. PPA
can automatically resolve all sort of relations you want to define. Fetched rows will be converted automatically to real objects. Below is described how you can annotate the class-properties appropriate.
OneToOne-Relations occur, when one parent record or field has either zero or one child record only. Respectively, when one Entity has a property which value is represented by null or another Entity.
This case is can be represented by annotations as follows:
/**
* @Column(name="role_id");
* @oneToOne(fetch="eager", mappedBy = "_PPA_examples_entity_Role")
*/
private $role;
- Firstly, you need to tell
PPA
which column is represented by the Entity, by the @column annotation. This column will mostly have a foreign key and refer to a primary value of another table. - Secondly, you must define the relation type and declare the fetch-type and the class to be mapped. Look here for info.
/**
* @oneToMany(fetch = "lazy", mappedBy = "_PPA_examples_entity_OrderPosition")
* @joinTable(x_column = "order_id")
*/
protected $orderPos;
/**
* @manyToMany(fetch = "lazy", mappedBy = "_PPA_examples_entity_Right")
* @joinTable(name = "role2right", column = "role_id", x_column = "right_id")
*/
private $rights = array();