legrand / sparqlmodel
用于Laravel 4的模型,可以通过SPARQL端点管理图形数据库中的对象。
dev-master
2013-07-31 09:42 UTC
Requires
- php: >=5.3.0
- laravel/framework: 4.0.*
- legrand/sparql: dev-master
This package is not auto-updated.
Last update: 2024-09-23 13:25:56 UTC
README
用于Laravel 4的模型,可以通过SPARQL端点管理图形数据库中的对象。
如何使用此模型
首先,您需要访问一个端点。如果您需要执行INSERT和DELETE操作,请确保您有这些权限。
安装
要在您的Laravel 4项目中安装SPARQLModel,请更新您的composer.json文件,并在require数组中添加以下行
"legrand/sparqlmodel": "dev-master"
之后,您可以在项目的根目录中打开一个终端,并运行composer
composer update
这将向您的项目中添加该软件包。
配置
此模型需要一些配置才能工作。在app/config中创建一个'sparqlmodel.php'文件,并添加以下数组
<?php
return array(
'endpoint' => 'https://:8890/sparql',
'graph' => 'https://:8890/DAV',
'status' => 'http://uri.for/property/status',
'created' => 'http://uri.for/property/created',
'updated' => 'http://uri.for/property/updated'
);
endpoint是用于通过SPARQL与图形数据库通信的URL,而graph是您用于应用程序的图的URI。
示例
现在您可以创建一个简单的模型。例如,这里是一个Like模型
use Legrand\SPARQLModel;
class Like extends SPARQLModel {
public $hash = null;
protected static $baseURI = "http://semreco/like/";
protected static $type = "http://semreco/class/Like";
protected static $mapping = [
'http://semreco/property/resource' => 'topic',
'http://semreco/property/performed' => 'performed'
];
public function generateID()
{
if(!isset($this->hash) || !is_string($this->hash) || $this->hash == '') throw new Exception("There is no hash string to generate the unique URI");
return $this::$baseURI . $this->hash;
}
public function save($moreData=[])
{
if(!isset($this->performed)) $this->performed = date('Y-m-d H:i:s', time());
parent::save($moreData);
}
}
这里您可以指定一些属性
- $baseURI:这里您给出URI的基础,标识like的部分将附加到这个字符串的末尾
- $type:这里您给出定义对象的类URI
- $mapping:您在这里给出一个数组,其中属性URI对应于属性名称,例如,为了获取like的日期,您可以这样做$like->performed
这里也有一些被覆盖的方法
- generateID():由于数据id是URI,我们无法生成一个唯一的id(例如,使用递增),因此您需要在这里返回对应于您的资源的URI
- save($moreData=[]):此方法已被覆盖以自动保存此like的日期
现在我们可以创建一个User模型
<?php
use Legrand\SPARQLModel;
class User extends SPARQLModel {
protected static $baseURI = "http://semreco/person/";
protected static $type = "http://xmlns.com/foaf/Person";
protected static $mapping = [
'http://xmlns.com/foaf/lastName' => 'lastname',
'http://xmlns.com/foaf/firstName' => 'firstname',
'http://xmlns.com/foaf/mbox' => 'email'
];
protected static $multiMapping = [
'http://semreco/property/like' => [
'property' => 'likes',
'mapping' => 'Like', //should be the name of the corresponding class
'order' => ['DESC', 'performed'], //performed is in the mapping of the Like model
'limit' => 5
]
];
public function generateID()
{
if(!isset($this->username)) throw new Exception("There is no username to generate the unique URI");
return $this::$baseURI . $this->username;
}
public function addLike($uri)
{
if(!$this->inStore) return; //isStore is usefull to see if the current object exist in database
$like = new Like();
$ex = explode('/', $uri);
$like->hash = $ex[count($ex)-1] . '_(' . $this->username . ')';
$like->topic = $uri;
$like->save(); // we save in database the like object
if(!isset($this->likes) || !is_array($this->likes)) $this->likes = [];
$this->likes[] = $like;
$this->link($like); //here we add the like to the current user, this method will user the $multiMapping attribute
}
}
这里另一个属性用于在用户及其like之间建立链接
- $multiMapping:需要一个复杂的数组。属性的URI需要另一个数组,具有以下格式:- property:属性的名称 - mapping:类的名称 - order:一个数组,其中第一个值是排序类型,第二个值是属性的名称 - limit:要获取的资源数量
您还可以看到,addLike方法获取用户所喜欢的资源的URI,以创建一个like并将其添加到用户的like中。
用法
现在我们可以使用我们的模型以这种方式使用来获取一个现有的用户
$user = new User::find('damien_legrand'); // Same as new User::find('http://semreco/person/damien_legrand');
$user->addLike('http://dbpedia.org/resource/Nine_Inch_Nails');
return $user->firstname; // will return 'Damien'
或创建一个用户
$user = new User();
$user->firstname = "Damien";
$user->lastname = "Legrand";
$user->save(); // save the user in database
更多内容将陆续推出
- 更好的文档
- 创建和
更新日期管理 - WHERE条件
- ...