PHP 工具,用于处理您的应用程序模型

v0.5.6 2017-06-22 12:50 UTC

This package is auto-updated.

Last update: 2024-09-13 02:58:57 UTC


README

警告

此实用工具仍处于开发中,尽管它可能以某种方式工作,但仍有大量代码缺失!请在版本 1.0 发布之前不建议使用此工具!

此实用工具提供处理应用程序模型的轻量级解决方案 - 您只需创建一个模型 YAML 配置文件,Dobee 就会完成剩余工作(创建数据库、生成实体 PHP 类以及让您查询它们,它还可以处理模型更新和基本继承)。不推荐在大型项目中使用 Dobee,因为您可能会达到限制。

安装

  1. 使用 composer 下载
	{
	    "require": {
	        "wernerdweight/dobee": "~0.5"
	    }
	}
  1. 在您的项目中初始化 Dobee
	<?php

	$dobee = new \WernerDweight\Dobee\Dobee('path/to/configuration/file.yml','path/to/strore/generated/entities','Your\\Namespace\\To\\Generated\\Entities');
  1. 设置配置

这只是一个演示配置,您可能需要编写自己的。

# path/to/configuration/file.yml
db:
    host: 127.0.0.1
    database: your_database_name
    user: root
    password: null
    port: null
model:
    base:
        abstract: ~
        softDeletable: ~
        loggable: ~
        blameable:
            targetEntity: author
            property: authorId
            nullValue: 0
        primary: id
        properties:
            id:
                type: int
                primary: ~
                notNull: true
            dateCreated:
                type: datetime
                notNull: true
                default: CURRENT_TIMESTAMP
            deleted:
                type: bool
                notNull: true
                default: 0
            authorId:
                type: int
                notNull: true
        defaultOrderBy:
            dateCreated: desc
    content:
        abstract: ~
        extends: base
        properties:
            title:
                type: string
                length: 255
                notNull: true
    article:
        extends: content
        properties:
            perex:
                type: text
                length: 510
            text:
                type: text
        relations:
            category: <<MANY_TO_MANY
            author: MANY_TO_ONE
    category:
        extends: content
        relations:
            article: MANY_TO_MANY
    author:
        extends: base
        properties:
            firstName:
                type: string
                length: 80
                notNull: true
            lastName:
                type: string
                length: 80
                notNull: true
        relations:
            article: ONE_TO_MANY
            address: <<ONE_TO_ONE
        defaultOrderBy:
            lastName: asc
    address:
        extends: base
        properties:
            street2:
                type: string
                length: 80
                notNull: true
            street:
                type: string
                length: 255
                notNull: true
            city:
                type: string
                length: 80
                notNull: true
        relations:
            author: ONE_TO_ONE
    image:
        extends: content
        properties:
            path:
                type: string
                length: 255
                notNull: true
            filesize:
                type: int
                notNull: true
        relations:
            imageFolder: MANY_TO_ONE
    imageFolder:
        extends: content
        relations:
            image: ONE_TO_MANY
            imageFolder: SELF::ONE_TO_MANY

用法

从 YAML 配置生成数据库

		$options = [
			'--dump',	/// outputs SQL for changes to be made to the database
			'--force'	/// forces changes to the database and regenerates php classes
            '--generate-entities'   /// forces regeneration of entity classes even if no changes were made to the model (must be used together with --force)
			/// no more options available at the moment
		];
		
		$dobee->generate($options);

查询实体

		$db = $dobee->getProvider();

		/// FETCH SINGLE RESULT
		/// 'article' stands for entity name as configured in YAML configuration file
		/// 1 stands for ID of requested article
		$article = $dp->fetchOne('article',1);		/// will return either object of class Article or null

		/// sample of how to access properties of an Article (confront configuration above)
		echo $article->getTitle();
		echo $article->getAuthor()->getFirstName();	/// author is lazy-loaded from database when needed
		if(!is_null($article->getCategories())){	/// categories are lazy-loaded from database when needed
			foreach($article->getCategories() as $category){
				echo $category->getTitle();
			}
		}

		/// FETCH MULTIPLE RESULTS
		/// options are not mandatory - if omitted all items will be loaded form the database
		$options = array(
			'leftJoin' => array(
				'this.author' => 'author',
				'author.address' => 'adress'
			),
			'where' => array(
				'this.title' => array(
					'operator' => 'like',
					'value' => '%microbe%'
				)
				'this.id' => array(
					'operator' => 'gte',
					'value' => 4
				)
			),
			'order' => array(
				'this.title' => 'asc',
				'this.id' => 'desc'
			),
			'limit' => array(
				'firstResult' => 0,
				'maxResults' => 10
			)
		);

		$articles = $db->fetch('article',$options);

许可协议

此实用工具受 MIT 许可协议保护。请参阅根目录中的完整许可协议。