Codeigniter & Doctrine

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Ijstheefles
  • Registratie: December 2011
  • Laatst online: 25-08 22:56
Hoi medetweakers,

Zit nu een tijdje vast op de integratie tussen CodeIgniter & Doctrine.

Zover ben ik:
- in libraries heb ik Doctrine.php gemikt, alsmede de gehele folder daarvan.
- commandline versie van doctrine doet wat 'ie moet doen.
- Doctrine toegevoegd aan autoload array van libraries

Huidige directory structuur:
application
- models
- entity
- Proxy
- Repositories

in entity de gegenereerde klasses met getters & setters, zonder annotaties
in Proxy de classes met annotaties
in Repositorys de repository klasses per entity (of proxy, is hoe je het bekijkt)

Vervolgens de volgende paginas gevonden:

Doctrine Project CodeIgniter pagina

CodeIgniter forum

En nog vele github links die ik achterwege laat. (Veelal hetzelfde als de Doctrine link)

Mijn huidige Doctrine.php in de libraries folder:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger;

define('DEBUGGING', FALSE);

class Doctrine {

    public $em = null;

    public function __construct()
    {
        // load database configuration and custom config from CodeIgniter
        require APPPATH . 'config/database.php';

        // Set up class loading.
        require_once APPPATH . 'libraries/Doctrine/Common/ClassLoader.php';

        $doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH . 'libraries');
        $doctrineClassLoader->register();

        $entitiesClassLoader = new \Doctrine\Common\ClassLoader('models', APPPATH . 'models/entity');
        $entitiesClassLoader->register();

        $proxiesClassLoader = new \Doctrine\Common\ClassLoader('Proxies', APPPATH . 'models/Proxies');
        $proxiesClassLoader->register();

        $symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', APPPATH . 'libraries/Doctrine');
        $symfonyClassLoader->register();

        // Choose caching method based on application mode
        if (ENVIRONMENT == 'production')
        {
            $cache = new \Doctrine\Common\Cache\ApcCache;
        }
        else
        {
            $cache = new \Doctrine\Common\Cache\ArrayCache;
        }

        // Set some configuration options
        $config = new Configuration;

        // Metadata driver
        $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH . 'models/entity'));
        $config->setMetadataDriverImpl($driverImpl);

        // Caching
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        // Proxies
        $config->setProxyDir(APPPATH . 'models/Proxies');
        $config->setProxyNamespace('Proxies');

        if (ENVIRONMENT == 'development') {
            $config->setAutoGenerateProxyClasses(TRUE);
        } else {
            $config->setAutoGenerateProxyClasses(FALSE);
        }

        // SQL query logger
        if (DEBUGGING)
        {
            $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger;
            $config->setSQLLogger($logger);
        }

        // Database connection information
        $connectionOptions = array(
            'driver'        => 'pdo_mysql',
            'user'          => $db['default']['username'],
            'password'      => $db['default']['password'],
            'host'          => $db['default']['hostname'],
            'dbname'        => $db['default']['database'],
            'charset'       => $db['default']['char_set'],
            'driverOptions' => array( 
                'charset'   => $db['default']['char_set'] 
            )
        );

        // Create EntityManager
        $this->em = EntityManager::create($connectionOptions, $config);
    }
}


En mijn simpele controller:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

use entity\Page;

class Homepage extends MY_Controller {

    public function index()
    {

        $data['content'] = 'Homepage/index';

        $data['pageinfo'] = $this->setPageInfo();

        $x = new Page();
        $x->setTitle("test");
        $x->setKeywords("a, bee, cee, dee");
        $x->setDescription("test");
        $x->setAuthor("test");
        $x->setInMenu(1);
        $x->setParent(0);
        $x->setLink("/");
        $x->setDeleted(0);

        $this->em->persist($x);
        $this->em->flush();

        echo 'saved';
         $this->renderView($data);
    }
}


En allicht nog handig; het relevante deel van de Page klasse (in models/entity/Page.php)
PHP:
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace Proxies;

use \Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity(repositoryClass="Repositories\PageRepository")
 * @Table(name="PAGE")
 */
class Page


En. Het. Doet. Compleet. Niets.

Zien jullie de fout?

De regel
PHP:
1
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
in de codeigniter link verschilt met mijn huidige config, ik zie alleen niet in waarom dit zou werken.

Overigens, tot nu toe heb ik al mijn tests gedaan zowel met die regel als met mijn versie van die regel.

Nog ideeen op de vrijdag avond?

Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Error-reporting staat aan?

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • Ijstheefles
  • Registratie: December 2011
  • Laatst online: 25-08 22:56
PHP:
1
define('ENVIRONMENT', 'development');

en
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'development':
            error_reporting(E_ALL);
        break;
    
        case 'testing':
        case 'production':
            error_reporting(0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
}


Ja dus! :)

Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Vooraan beginnen: definieer "doet niets". Werkt een simpele echo 'hoi';, zonder framework e.d., wel? En/of een phpinfo();? En mét framework? Is er misschien wel output maar zie je 't niet door incorrecte HTML? M.a.w. zie je wel iets als je 'view source' doet in je browser? Wordt je controller wel aangeroepen? Etc. etc. Debuggen

[ Voor 19% gewijzigd door RobIII op 12-05-2012 02:51 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • Ijstheefles
  • Registratie: December 2011
  • Laatst online: 25-08 22:56
Oeps, je hebt gelijk. Sorry... ik post morgen (en anders overmorgen) meer details.

Gaat een beetje moeilijk, ben jarig, dan wordt het blijkbaar niet fijn gevonden als ik ga zitten programmeren =p

en morgen moederdag.

Ik kom hier nog op terug! >.<

Acties:
  • 0 Henk 'm!

  • Cartman!
  • Registratie: April 2000
  • Niet online
Je vergeet nog:

PHP:
1
ini_set('display_errors', true);


en irritant genoeg is E_ALL niet daadwerkelijk alles dus kun je beter error_reporting(-1) gebruiken om alle errors te krijgen.

Acties:
  • 0 Henk 'm!

  • Bananenspin
  • Registratie: December 2008
  • Laatst online: 13-08 15:52

Bananenspin

Omdat het kan

Als ik de codeigniter wiki over doctrine erbij pak is één van de eerste regels: First we must get the source of Doctrine from svn and place it in the system/database folder.

Terwijl jij dit in de library folder hebt geplaatst?

HOI.


Acties:
  • 0 Henk 'm!

  • Ijstheefles
  • Registratie: December 2011
  • Laatst online: 25-08 22:56
Goed, iets later als gepland:

Inmiddels doet het al wat meer, het probleem nu is het volgende:

Doctrine.php in application/libraries folder:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger;

define('DEBUGGING', FALSE);

class Doctrine {

    const ENTITY_DIR = 'models/Entities';
    const PROXY_DIR = 'models/Proxies';

    public $em = null;

    public function __construct()
    {
        // load database configuration and custom config from CodeIgniter
        require APPPATH . 'config/database.php';

        // Set up class loading.
        require_once APPPATH . 'libraries/Doctrine/Common/ClassLoader.php';

        $doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH . 'libraries');
        $doctrineClassLoader->register();

        $entitiesClassLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH . self::ENTITY_DIR);
        $entitiesClassLoader->register();

        $proxiesClassLoader = new \Doctrine\Common\ClassLoader('Proxies', APPPATH . self::PROXY_DIR);
        $proxiesClassLoader->register();

        $symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', APPPATH . 'libraries/Doctrine');
        $symfonyClassLoader->register();

        // Choose caching method based on application mode
        if (ENVIRONMENT == 'production')
        {
            $cache = new \Doctrine\Common\Cache\ApcCache;
        }
        else
        {
            $cache = new \Doctrine\Common\Cache\ArrayCache;
        }

        // Set some configuration options
        $config = new Configuration;

        // Metadata driver
        $driverImpl = $config->newDefaultAnnotationDriver(APPPATH . self::PROXY_DIR);
        $config->setMetadataDriverImpl($driverImpl);

        // Caching
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        // Proxies
        $config->setProxyDir(APPPATH . self::PROXY_DIR);
        $config->setProxyNamespace('Proxies');

        if (ENVIRONMENT == 'development') {
            $config->setAutoGenerateProxyClasses(TRUE);
        } else {
            $config->setAutoGenerateProxyClasses(FALSE);
        }

        // SQL query logger
        if (DEBUGGING)
        {
            $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger;
            $config->setSQLLogger($logger);
        }

        // Database connection information
        $connectionOptions  = array(
            'driver'        => 'pdo_mysql',
            'user'          => $db['default']['username'],
            'password'      => $db['default']['password'],
            'host'          => $db['default']['hostname'],
            'dbname'        => $db['default']['database'],
            'charset'       => $db['default']['char_set'],
            'driverOptions' => array( 
                'charset'   => $db['default']['char_set'] 
            )
        );

        // Create EntityManager
        $this->em = EntityManager::create($connectionOptions, $config);
    }
}


in controller:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Homepage extends MY_Controller
{
    public function index()
    {
        $x = new Entities\classnaamhier();

        $this->doctrine->em->persist($x);
        var_dump($x);
        $this->doctrine->em->flush();
    }
}


Huidige directory structuur:
  • application
    • models
      • Entities
      • Proxies
      • Repositories
Als ik in de Entites map nog een map 'Entities' maak, doet het het wel, alleen krijg ik dan
code:
1
Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Entities\classhier is not a valid entity or mapped super class.'


Als ik vervolgens in de die class de doctrine annotations zet die eigenlijk in de Proxies horen, dan doet het het. Alleen weet ik dan niet hoe ik de directory structuur netjes maak. Duplicate 'Entities' mappen en geen verdere scheiding (Bijvoorbeeld subfoldering per klas met proxy/entitiy/repositorys ofzo).

Plus het feit dat ik dan 1 klas heb wat zowel de proxy als de entity moet voorstellen, wat zo ongeveer tegen elk principe in gaat dat ik bedenken kan.

Iemand nog ideeen? ;x

Ik heb 't werkend. Ik post vanavond mijn oplossing! (ff eten eerst ;x )
Pagina: 1