- Introduction
- Setting up the server
- Creating the entities
- Creating the database schema
- Loading the entities
- Creating new entities
- Deleting entities
- Updating entities
- Conclusion
Now that Flextrine has been configured we need to create our entities. Entities are the objects that we want to enable for reading and writing to the database, and they are defined in PHP in the entities directory of the Flextrine server side component.
The requirements for our simple address book are that we need to have groups containing contacts. Unfortunately Group is a reserved word in SQL so we’ll call it ContactGroup. ContactGroups have a name, and many Contacts, and Contacts have a name, telephone number, birthday and a contactGroup. We are going to place all our entities in the **vo **package. This gives us the class diagram below:
Create a folder called vo in the entities directory of the Flextrine server side component and create ContactGroup.php and Contact.php with the contents given below. More information on writing entities can be found in the Doctrine documentation for Basic Mapping and Association Mapping. The main thing to remember when creating entities is that:
- All mapped attributes must be public
ContactGroup.php
<?php
namespace vo;
use DoctrineCommonCollectionsArrayCollection;
/**
* @Entity
*/
class ContactGroup {
/** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */
public $id;
/** @Column(length=100, type="string") */
public $name;
/**
* @OneToMany(targetEntity="Contact", mappedBy="contactGroup")
*/
public $contacts;
public function __construct() {
$this->contacts = new ArrayCollection();
}
}
Contact.php
<?php
namespace vo;
use DoctrineCommonCollectionsArrayCollection;
/**
* @Entity
*/
class Contact {
/** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */
public $id;
/** @Column(length=80, type="string") */
public $name;
/** @Column(length=50, type="string", nullable="true") */
public $telephoneNumber;
/** @Column(type="date", nullable="true") */
public $birthday;
/**
* @ManyToOne(targetEntity="ContactGroup", inversedBy="contacts")
*/
public $contactGroup;
public function __construct() {
}
}
Now that we have created our entity definitions we need to create the matching database schema.