您的包的简要描述

v0.0.1 2024-05-04 11:31 UTC

This package is auto-updated.

Last update: 2024-09-04 12:13:00 UTC


README

Entify 是一个易于使用的 ORM 工具,可以根据 YAML 文件创建实体和数据库表。

如何使用 entify

创建配置文件:在您项目的根目录下创建一个名为 entify-config.php 的文件。配置必须类似于以下内容

<?php

$config = [
    'entities' => [
        'Src\\Entities' => 'src/entities'
    ],

    'schema' => '',
    'default-data' => '',
    
    'database-connection' => [
        'DB_HOST' => '',
        'DB_USER' => '',
        'DB_PASS' => '',
        'DB_NAME' => '',
        'DB_PORT' => 3306
    ]
];

return $config;

创建架构文件:在您项目的任何位置创建一个 YAML 文件,并在配置文件中指定其位置,位于;架构下。

架构可能的样子示例

users: 
  columns:
    id: {type: int, primary: true, auto_increment: true}
    username: {type: varchar, length: 12}
    password: {type: varchar, length: 255}
  relations:
    usernotes: {type: oneToMany, local: id, foreign: user_id}

notes:
  columns:
    id: {type: int, primary: true, auto_increment: true}
    title: {type: varchar, length: 255}
    content: {type: text}
  relations:
    usernotes: {type: oneToMany, local: id, foreign: note_id}

usernotes:
  columns:
    id: {type: int, primary: true, auto_increment: true}
    user_id: {type: int}
    note_id: {type: int}
  relations:
    users: {type: manyToOne, local: user_id, foreign: id}
    notes: {type: manyToOne, local: note_id, foreign: id}

向数据库添加默认数据:在您项目的任何位置创建一个普通的 PHP 文件,并在配置文件中指定其位置,位于;默认数据下。

默认数据文件可能的样子

<?php

require 'vendor/autoload.php';

use Src\Entities\Notes;
use Src\Entities\Usernotes;
use Src\Entities\Users;

$users = new Users();
$users->setUsername('admin');
$users->setPassword(password_hash('admin', PASSWORD_DEFAULT));
$users->create();

$note = new Notes();
$note->setTitle('Note 1');
$note->setContent('Content 1');
$note->create();

$userNote = new Usernotes();
$userNote->setUser_id($users->getId());
$userNote->setNote_id($note->getId());
$userNote->create();

命令列表及其功能:

  • vendor/bin/entify entify --all (创建数据库表及其对应的实体)

  • vendor/bin/entify entify --entities (仅创建实体)

  • vendor/bin/entify entify --database (仅添加表及其关系至数据库)

  • vendor/bin/entify entify --drop (删除所有数据库表和实体)

  • vendor/bin/entify entify --refresh (删除数据库和实体并重新创建)