2

I'm working on a school project and i want to use Doctrine with it but without any framework because they are usually too big. The problem is that i can't integrate Doctrine even following the configuration tutorial of documentation (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html) . Please, if anyone did have to setup it and have some tips or ideas about the Doctrine configuration (or in which file it goes), i take anything. Thanks by advance

phndiaye
  • 309
  • 4
  • 19
  • Could you give more detail about the problems that you're running into, error messages, etc.? – Andrew Mar 05 '13 at 15:36

1 Answers1

2

How're you? I was having the same problem theese days and the solution was: 1) configure a "boostrap.php" file mentioned that creates the entityManager, annotationReader and other things like this:

   use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;

require_once __DIR__ . '/vendor/autoload.php';
//require_once __DIR__ . "/src/model/persistence/entities";

$paths            = array(__DIR__ . "src/model/persistence/entities/");
$isDevMode        = false;
$connectionParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => 'pwd',
    'dbname'   => 'db_name',
);

$config = Setup::createConfiguration($isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);

// registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);

$entityManager = EntityManager::create($connectionParams, $config);

2) I'm using MVC with an index.php as access point, so I include this "bootstrap.php" at the beginning and load $entityManager in a global array (let's say $registry) so a can pass it to the model (in costructor for example) and then to the dao so i can access to it.

3) Use $use the $entityManager in the dao to create and excecute queries like the following:

<?php

namespace persistence\daos;

require_once "vendor/autoload.php";
include_once __PERSISTENCE_PATH . '/daos/Dao.php';
include_once __PERSISTENCE_PATH . '/entities/UsuarioEntity.php';
include_once __PERSISTENCE_PATH . '/entities/RolUsuarioEntity.php';

use persistence\daos\Dao;
use persistence\daos\DaoImpl;
//use persistence\entities\RolUsuario;


interface UsuarioDao extends Dao {

    function findAllRoles();

    public function findById($id);

}

class UsuarioDaoImpl extends DaoImpl implements UsuarioDao {

    public function __construct($em) {
        parent::__construct($em);
    }

    public function findById($id) {
        return parent::findByPrimaryKey('model\persistence\entities\Usuario', $id);
    }

    public function save($usuario) {
        parent::save($usuario);
    }

    public function delete($usuario) {
        parent::delete($usuario);
    }

    public function findAll() {
        return parent::findAllByEntity('model\persistence\entities\Usuario');
    }

    public function login($email, $pwd) {
        $query = $this->entityManager->createQuery("SELECT u FROM model\persistence\entities\Usuario u WHERE u.email = '$email' AND u.senha = '$pwd' ");
        return $query->getResult();
    }

    public function findAllRoles() {
        $rolesRepository = $this->entityManager->getRepository('model\persistence\entities\RolUsuario');
        return $rolesRepository->findAll();
    }

}

Al this thing worked fine for me without any of the known Frameworks.

THIS post helped a lot throw the process, take a look at this too.

Hope it works with you my friend!! Cheers and sorry for my english XD!

Community
  • 1
  • 1
MRodriguez08
  • 189
  • 1
  • 7