webd / noorm
此包的最新版本(0.2)没有可用的许可信息。
无ORM的PHP对象存储
0.2
2015-09-10 12:57 UTC
Requires
- zeptech/annotations: 1.1.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2024-09-19 22:09:53 UTC
README
无ORM的PHP对象存储。
PHP-Noorm使持久化PHP对象变得简单,无需ORM(对象-关系映射)的开销。
安装
使用composer安装最新版本
$ composer require webd/noorm
快速入门
首先,加载composer,并定义一个目录,您的对象可以在此目录中保存(它必须是可写的)。
require_once __DIR__ . "/../vendor/autoload.php"; use noorm\Persistent; // Indicate where to save data Persistent::SetDirectory("/tmp/noorm-example");
定义您想要持久化的类。它们必须扩展类\noorm\Persitent。
class Client extends Persistent { public $name = ""; private $val; /** * Use annotations to indicate $items is a many-to-many relation to class Item * @target Item * @var \noorm\ManyToManyRelation */ public $items; public function SetName($name) { $this->name = $name; return $this; } public function GetVal() { return $this->val; } public function SetVal($val) { $this->val = $val; return $this; } } class Item extends Persistent { public $name = ""; /** * Use annotations to indicate $clients is a many-to-many relation to Client * @target Client * @var \noorm\ManyToManyRelation */ public $clients; }
现在,您可以创建对象并将它们保存到磁盘上。
// Create a new object and save it to disk $client = new Client(); $client->name = "C1"; $client->Save();
静态方法所有(All)返回一个表示此类所有已保存对象的\noorm\Dataset。您可以使用此数据集来过滤、排序或列出您的对象。
// Show all clients /* @var $client Client */ foreach (Client::All()->Collect() as $client) { echo $client->name . " : "; echo $client->items->Get()->Count() . " items\n"; }
PHP-Noorm还为您管理多对多关系。这些关系使用类定义中的注释来描述。
// Create a new item $item = new Item(); $item->name = "I"; $item->Save(); // Add this item to the first client $client = Client::All()->First(); $client->items->Add($item);
已知的错误和限制
以下为计划改进的内容
- 两个类之间只能有一个多对多关系;
- 您不能定义一个类与自身的多对多关系;
- 所有(All)将贪婪加载所有对象,这很耗费内存。