hypersistence2-0/hypersistence-laravel

PHP面向对象持久化框架,用于Laravel。

v2.8.12 2022-10-19 18:59 UTC

This package is auto-updated.

Last update: 2024-09-19 23:19:46 UTC


README

PHP面向对象持久化框架。

要在Laravel 5.4中使用,请将以下行添加到config/app.php -> providers中

    Hypersistence\HypersistenceServiceProvider::class,
    Hypersistence\Auth\HypersistenceAuthServiceProvider::class,

要使用命令从数据库创建模型

    php artisan hypersistence:make-models <directory?> {--override}

要为认证创建基本用户模型,请使用此命令

    php artisan hypersistence:make-auth

使用文档注释标签映射与数据库的类。

示例

/**
 * @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;
    }
}

加载示例

$p = new Person();
$p->setId(1);
$p->load();

echo $p->getName();

保存示例

$p = new Person();
$p->setName('Mateus Fornari');
$city = new City(1);
$city->load();
$p->setCity($city);
$p->save();

搜索示例

$p = new Person();
$p->setName('Mateus');

$search = $p->search();
$search->orderBy('name', 'asc');
$search->orderBy('city.name', 'desc');

$list = $search->execute();