meridius/dbentities-generator

用于与 Nette Database 一起使用的数据库实体生成器

1.0.4 2015-07-25 14:52 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:58:11 UTC


README

用于与 Nette Database 一起使用的数据库实体生成器

当您处理 Nette Database(以及其他类似的数据库框架)时,是否厌倦了手动输入表和列名?现在您不必再这样做!

为什么我要使用它

  • 简单的设置
  • 简单使用
  • IDE 自动提示列和表名
  • 列/变量类型提示
  • 不再使用难以追踪的字符串来引用表/列名
  • 更简单的表连接

如何设置

只需运行生成器的 php index.php -s path/to/your_scheme.sql,它将解析给定的方案 SQL 文件并创建相应的实体文件。

其他选项

-s <path>             SQL file with schema to be parsed to entities.
-n <namespace>        What namespace to put generated entities into. 
                      Will be used also as destination directory.
                        (default: DbEntity)
-d <database name>    Used as part of namespace and directory for entities.
                        [optional] (default: none)
-a                    Generate also absolute constants. This will generate: 
                        const __COLUMN_NAME = 'table.column_name';
                      Constant name is prefixed with (__) two underscores.
                        [optional] (default: true)
-e                    Enquote table and column names. This will generate: 
                        const __COLUMN_NAME = '`table`.`column_name`';
                        [optional] (default: false)
-f                    Remove destination directory if exists - use force.
                        [optional] (default: true)
-h | --help           This help.

生成的实体的使用

在哪里可以使用它们

生成的数据库实体的主要用途是针对基于 Nette(及其衍生)框架并使用 Nette Database 的项目。

其他框架的先决条件

如果您决定使用 Nette 以外的框架或没有任何框架的实体,请注意,您需要将这些两个组件包含到您的项目中

示例使用

  • 您可以直接设置实体属性的值。
  • 函数 $entity->getArray() 将从实体获取关联数组,为 Nette Databases 的 ->insert()->update() 函数准备好。
  • ActiveRow 对象传递给实体构造函数将根据 ActiveRow 中的值设置该实体的属性。

创建新记录

$movie = new Movie;
$movie->name = $values["name"];
$movie->year = $values["year"];
$movieManager->create($movie);

# ---------- MovieManager ----------
public function create(Movie $movie) {
	$this->db->table($movie->getTableName())->insert($movie->getArray());
}

获取现有记录

$movie = $movieManager->getByName($values["name"]);
echo $movie->year;

# ---------- MovieManager ----------
public function getByName($name) {
	$row = $this->db->table(Movie::getTableName())->where(array(
			Movie::NAME => $name,
		))->fetch();
	return ($row instanceof ActiveRow) ? new Movie($row) : null;
}

通过来自另一个表的值获取现有记录

public function getByTagName($tagTitle) {
	$row = $this->db->table(Movie::getTableName())
		->select(Movie::getTableName() . '.*')
		->where(array(
			Tag::__TITLE => $tagTitle, // see (-a) option for description
		))->fetch();
	return ($row instanceof ActiveRow) ? new Movie($row) : null;
}

注意

生成器将解析 MySQL 文件,其他数据库类型的语法尚未测试。