felixonline / base
Felix Online 基础服务
0.9.1
2016-11-06 22:29 UTC
Requires
- jv2222/ezsql: dev-master
- mrclay/minify: 2.3.*
- oyejorge/less.php: 1.7.*
- robmorgan/phinx: 0.6.*
- rzekanet/service-akismet: 1.*
- swiftmailer/swiftmailer: 5.*
- tedivm/stash: 0.14.*
Requires (Dev)
- fzaninotto/faker: 1.6.*
- phpunit/dbunit: >=1.2
- phpunit/phpunit: 5.6.*
- psy/psysh: @stable
- ptrofimov/xpmock: 1.1.*
README
Felix Online 可重复使用的应用程序框架。
BaseApp 为 Web 和 CLI 应用程序提供数据库 ORM 和辅助类。
TODO: 在 gh-pages 上编写文档
数据库概念
模型
模型负责封装 Core 中特定实体的所有操作,例如文章或类别。这主要对应于特定的数据库表,但模型可以与其他表有关联。
每个数据库模型都继承自 BaseDB
并定义了一个与数据库表列对应的字段列表。
namespace FelixOnline\Core; use FelixOnline\Core\Type; class Foo extends BaseDB { public $dbtable = 'foo'; public function __construct($id = NULL) { $fields = array( 'text_field' => new Type\CharField(), 'datetime' => new Type\DateTimeField(), ); parent::__construct($fields, $id); } } // get a model that already exists $foo = new Foo(1); // Get model values $foo->getTextField(); // 'text_field' column value $foo->getDatetime(); // 'datetime' column as a timestamp // or create an empty model $fizz = new Foo(); // set values $fizz->setTextField('Hello World'); // save the model to the database $fizz->save();
如果模型没有数据库表作为后盾,您也可以从 BaseModel
继承。
外键
模型字段可以通过其主键与另一个模型建立关系。如果是这种情况,则在检索该字段的值时,您将得到相应的模型。您也可以通过将关系模型作为参数传递给设置器来设置其值。
namespace FelixOnline\Core; use FelixOnline\Core\Type; class Foo extends BaseDB { public $dbtable = 'foo'; public function __construct($id = NULL) { $fields = array( 'bar' => new Type\ForeignKey('FelixOnline\Core\Bar'), ); parent::__construct($fields, $id); } } class Bar extends BaseDB { public $dbtable = 'bar'; public function __construct($id = NULL) { $fields = array( 'text' => new Type\CharField(), ); parent::__construct($fields, $id); } } /** * Table: foo * * +----+------+ * | id | bar | * +----+------+ * | 1 | 2 | * +----+------+ * * Table: bar * * +----+--------+ * | id | text | * +----+--------+ * | 1 | Fizz | * +----+--------+ * | 2 | Buzz | * +----+--------+ */ $foo = new Foo(1); // get foreign key model $bar = $foo->getBar(); // 'Bar' object $bar->getText(); // 'Buzz' // set a foreign key $bar = new Bar(1); $foo->setBar($bar); // set the 'bar' field on 'foo' $foo->getBar()->getText(); // 'Fizz'
管理器
管理器负责从数据库中选择具有可选过滤器的模型列表。某些模型具有特定的管理器,例如 ArticleManger
或 CatgoryManager
,因为它们还包含自定义方法。但是,可以通过构建方法动态创建表示数据库表和模型的通用管理器。请参见下文。
$manager = (new FelixOnline\Core\ArticleManager()) ->filter('published < NOW()') ->order('published', 'DESC'); $manager->count(); // get the number of models the manager will return $manager->limit(0, 10); // limit the number of models $manager->values(); // get an array of models // You can also join managers together $authorManager = (new \FelixOnline\Core\ArticleAuthorManager()) ->filter("author = 'felix'"); $manager->join($authorManager); $manager->values(); // articles by the user 'felix'
管理器构建器
您可以通过在 BaseManager
类上使用静态方法 build
动态创建管理器。
$author_manager = BaseManager::build( 'FelixOnline\Core\User', // model class 'article_author', // database table 'author' // primary key ); $author_manager->filter('article = %i', array(1)) ->values();
测试
- 运行
composer install
安装所有依赖项 - 在根目录下运行
./vendor/bin/phpunit tests
以运行测试