Package Data | |
---|---|
Maintainer Username: | damien_legrand |
Maintainer Contact: | legrand.damien.06@gmail.com (Damien Legrand) |
Package Create Date: | 2013-05-13 |
Package Last Update: | 2013-07-31 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:04:36 |
Package Statistics | |
---|---|
Total Downloads: | 24 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 7 |
Total Watchers: | 2 |
Total Forks: | 2 |
Total Open Issues: | 1 |
A model for Laravel 4, that can manage object through a graph database using SPARQL endpoint.
First of all you need to have access to an endpoint. If you need to perform INSERT and DELETE, make sur you have these privileges.
To install SPARQLModel in your Laravel 4 project, update your composer.json file by adding this line in the require array:
"legrand/sparqlmodel": "dev-master"
After that you can open a terminal in the root folder of your project and run composer :
composer update
it will add the package in your project.
This model need some configurations to work. Create a 'sparqlmodel.php' file in app/config and add this arry :
<?php
return array(
'endpoint' => 'http://localhost:8890/sparql',
'graph' => 'http://localhost:8890/DAV',
'status' => 'http://uri.for/property/status',
'created' => 'http://uri.for/property/created',
'updated' => 'http://uri.for/property/updated'
);
The endpoint is the URL to communicate with the graph database through SPARQL, and graph is the URI of the graph that you are using for your application.
Now you can create a simple model. For example here a Like model :
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);
}
}
Here you can some attributes that you need to specify
There are also some some methods that are overided here :
Now we can create a User model :
<?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
}
}
Here another attribut is used to make a link between the user and his likes :
You can see also that a method addLike get the uri of the resource that have been liked by the user to create a like and add it to the user's likes.
Now we can use our models likes this to get an existing user:
$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'
Or to create a user :
$user = new User();
$user->firstname = "Damien";
$user->lastname = "Legrand";
$user->save(); // save the user in database