PHP code example of anh / doctrine-extensions-resource
1. Go to this page and download the library: Download anh/doctrine-extensions-resource library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anh / doctrine-extensions-resource example snippets
$resources = [
'article' => [ // resource name
'model' => 'Some\Name\Space\Entity\Article', // model (l)
'interface' => 'Another\Lib\Interface', // for Doctrine ResolveTargetEntityListener (optional, can be array, not implemented yet)
'rules' => [ // rules for this resource (optional)
'isPublished' => [
'isDraft' => false,
'r.publishedSince <= current_timestamp()',
],
],
],
'category' => [ // another resource
/* ... */
],
];
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Anh\DoctrineResource\ORM\ResourceRepositoryFactory;
use Anh\DoctrineResource\ORM\EventListener\LoadMetadataSubscriber;
use Anh\Paginator\Paginator;
$repositoryFactory = new ResourceRepositoryFactory($resources, new Paginator());
// create config for object manager
$config = new Configuration();
/* set up orm config here */
$config->setRepositoryFactory($repositoryFactory);
// create entity manager
$entityManager = EntityManager::create([/* connection params */], $config);
// create event dispatcher
$eventDispatcher = new EventDispatcher();
/* set up event dispatcher here */
// add doctrine event subscriber
$entityManager->getEventManager()->addEventSubscriber(new LoadMetadataSubscriber($resources));
// create factory for resource manager
$resourceManagerFactory = new ResourceManagerFactory($resources, $eventDispatcher);
$articleManager = $resourceManagerFactory->create('article', $entityManager);
// basic CRUD operations
// create resource
$article = $articleManager->createResource();
$article->setTitle('This is so test title');
$articleManager->create($article);
// update resource
$article->setTitle('This is test title');
$articleManager->update($article);
// delete resource
$articleManager->delete($article);