How to Use a PHP Persistence Framework to Store Objects like Java Hibernate – Hypersistence package blog

In this article you will learn:

Introduction to the Hypersistence Object-Relational Mapping Framework

How to Create Annotation Comments to Define the Mapping of Objects into Database Tables

What Are the Hypersistence Tags to Define Object Mappings

How to Download the Hypersistence Package or Install It with PHP Composer

Introduction to the Hypersistence Object-Relational Mapping Framework

Hello, I`m Mateus Fornari, author of Hypersistence. In this post I will explain how to map your model class to database table. It`s very simple and save a lot of coding time.

If you have ever used Hibernate in Java, Hypersistence will looks familiar to you.

How to Create Annotation Comments to Define the Mapping of Objects into Database Tables

Hypersistence uses annotations in PHPdoc format that may be inserted in the comments of class. Those annotations can serve to define the mappings of the class and its properties to database table.

The first thing to do is to extend Hypersistence class. When your class extends the Hypersistence class, it inherits three importante methods: load, save, search and delete. Those methods will be called by your application to retrieve, store, list with filters and remove from database.

What Are the Hypersistence Tags to Define Object Mappings

Here follows a list of tags that you can use in comments of the classes that will be used to map data objects into records in a database table.

@table(<table_name>) – binds the class to database table.

@column(<column_name>) – binds a property to database column.

@primaryKey – sets property to be treated as primary key.

@oneToMany(<lazy or eager>) – used when the property representes a list in a on to many relationship. If the value is set to lazy, objects in the list will have only primary key configured and you need to call load method when you need. If the value is set to eager Hypersistence will call load method for each item.

@manyToOne(<lazy or eager>) – used when the property represents an object in a many to one relationship.

@joinColumn(<column_name>) – sets the column to make join in target table.

@itemClass(<class_name>) – the class of object represented by the property.

@manyToMany(<lazy or eager>) – used when the property represents a list in a many to many relationship.

@joinTable(<table_name>) – the name of junction table.

@inverseJoinColumn(<column_name>) – the name of column that joins with target table.

Example

In this example I will map a class (Person) to its respective database table.

/**

 * @table(person)
 */
class Person extends Hypersistence {

    /**
     * @primaryKey
     * @column(person_id)
     */
    private $id;

    /**
     * Use empty column when the column has the same name that var.
     * @column()
     */
    private $name;

    /**
     * When you have a Many to One relationship use tags as below.
     * You can use 'lazy' or 'eager'.
     * @manyToOne(lazy)
     * @column(city_id)
     * @itemClass(City)
     */
    private $city;

    /**
     * When you have a One to Many relationship use tags as below.
     * You can use 'lazy' or 'eager'.
     * @oneToMany(lazy)
     * @joinColumn(person_id)
     * @itemClass(Book)
     */
    private $books;

    /**
     * When you have a Many to Many relationship use tags as below.
     * You can use 'lazy' or 'eager'.
     * @manyToMany(lazy)
     * @joinColumn(person_id)
     * @inverseJoinColumn(course_id)
     * @itemClass(Course)
     * @joinTable(person_has_course)
     */
    private $courses;


    public function getId(){
        return $this->id;
    }

    public function setId($id){
        $this->id = $id;
    }

    public function getName(){
        return $this->name;
    }

    public function setName($name){
        $this->name = $name;
    }

    public function getCity(){
        return $this->city;
    }

    public function setCity($city){
        $this->city = $city;
    }

    public function getBooks(){
        return $this->books;
    }

    public function getCourses(){
        return $this->courses;
    }
}

The example above shows how to map only Person class. You have to map classes City, Book and Course.

How to Download the Hypersistence Package or Install It with PHP Composer

The Hypersistence package can be downloaded from download page or be installed using the PHP Composer tool following instructions in the Composer install instructions page.

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top