Skip to content

Hope with Doctrine

Pedro Faria edited this page Apr 14, 2017 · 5 revisions

Add Doctrine to your composer.json

{
    ...
    "require": {
        "pedrofaria/hope": "1.0.0",
        "doctrine/orm": "2.4.*",
        "symfony/yaml": "2.*"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

Create Doctrine Provider

app/Providers/DoctrineProvider.php

<?php
namespace App\Providers;

use Hope\Application;
use Hope\Contracts\ProviderInterface;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

class DoctrineProvider implements ProviderInterface
{
    public static function register(Application $app)
    {
        $app->bind('Doctrine\ORM\EntityManager', function() {
            // Create a simple "default" Doctrine ORM configuration for Annotations
            $isDevMode = true;
            $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/.."), $isDevMode);

            // database configuration parameters
            $conn = array(
                'driver' => 'pdo_sqlite',
                'path' => __DIR__ . '/../../db.sqlite',
            );

            // obtaining the entity manager
            return EntityManager::create($conn, $config);
        });
    }
}

Register your Provider

public/index.php

<?php

require __DIR__ . '/../vendor/autoload.php';

$app->bootstrap();

$app = new Hope\Application();

$app->addExternalProviders([
    App\Providers\DoctrineProvider::class,
]);

$app->setRoute(__DIR__ . '/../routes.php');

$app->bootstrap();
$app->run();

And that's it! now you can use it with Dependency Injection

Using Doctrine CLI

Create file cli-config.php

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Hope\Application();

$app->addExternalProviders([
    App\Providers\DoctrineProvider::class,
]);

$app->bootstrap();

$entityManager = $app->get('Doctrine\ORM\EntityManager');
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);

Now you can use the doctrine console vendor/bin/doctrine to support you development.

Create your Entities

app/Project.php

<?php
namespace App;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity @Table(name="projects")
 **/
class Project
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="integer") **/
    protected $jiraId;
    /** @Column(type="string") **/
    protected $name;
    /** @Column(type="string") **/
    protected $code;

    // Getters and Setters...
}

Use at your controller

app\Controller\ProjectsController.php

<?php
namespace App\Controller;

use Hope\Http\Controller;
use Hope\Http\Response;
use Doctrine\ORM\EntityManager;

class ProjectsController extends Controller
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function projects()
    {
        $projectRepository = $this->em->getRepository('App\Project');
        $projects = $projectRepository->findAll();

        $data = [];
        foreach ($projects as $project) {
            $data[] = [
                'code' => $project->getCode(),
                'issues' => $project->getIssues()
            ];
        }

        return $data;
    }
}

And that's it...