Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine 2 returns errror 'proxy directory must be writable'

i am using doctrine 2 with Zendframework 2

i am trying to return an object but keep getting this error:

Your proxy directory must be writable

this is my query:

$this->getEntityManager()
                    ->getRepository('Messages\Entity\Messages')
                    ->findOneBy(array('id' => 6,
                                      'receiverId' => 16
                   ));

However, the same query return an array without any problems re:

$qb = $this->getEntityManager()->createQueryBuilder();

     $qb->select(array('u'))
        ->from('Messages\Entity\Messages','u')
        ->where('u.id = :id')  
        ->andWhere('u.receiverUserId = :receiverId')    
        ->setParameter('receiverId',16)    
        ->setParameter('id',(int)6);

      $query = $qb->getQuery();
        return   $data = $query->getArrayResult(); 
like image 390
user3389187 Avatar asked May 01 '14 10:05

user3389187


2 Answers

If you are using Setup::createAnnotationMetadataConfiguration you can simply fix it by

  1. Create the following directory in your project root. data/DoctrineORMModule/Proxy
  2. chmod -R 755 data/DoctrineORMModule/Proxy

In your bootstrap include the path to the data directory as in:

Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src/models"), $this->isDevMode, "data/DoctrineORMModule/Proxy")

That fixed it for me.

like image 97
Emmanuel John Avatar answered Nov 15 '22 11:11

Emmanuel John


Proxies are simple classes which extends your actual entities and used internally by doctrine to hydrate associations of your entities by lazy-loading them. Doctrine decides to use or not to use a proxy instances on runtime for different situations and it really depends on your queries and associations in your entities. You may want to read about this subject in-depth at official documentation.

In your case, doctrine trying to generate a proxy class for your Messages entity but your proxy directory is simply not writable as error output's said.

This seems like misconfiguration of DoctineModule. (Assuming that you are using DoctrineORMModule to integrate doctrine with ZF2) Doctrine needs a writeable directory to put that generated proxy classes. For ZF2's view, data directory on application root perfectly fits for this requirement.

  1. Make sure existence of the line below in your public/index.php:

    chdir(dirname(__DIR__));
    
  2. and try to use a configuration like this:

    <?php
    /**
     * module/Application/config/module.config.php
     */
    return array(
        'router' => array(
             // ...
        ),
    
        // ...
    
        'doctrine' => array(
            'driver' => array(
               //...
            ),
    
            /**
             * Generating proxies on runtime and using array cache instead of apc(u)
             * greatly reduces the performance. So, you may want to override 
             * this settings on production environment.
             */
            'configuration' => array(
                'orm_default' => array(
                    'metadata_cache' => 'array',
                    'query_cache' => 'array',
                    'result_cache' => 'array',
                    'hydration_cache' => 'array',
                    'generate_proxies' => true,
                    'proxy_dir' => 'data/DoctrineORMModule/Proxy',
                    'proxy_namespace' => 'DoctrineORMModule\Proxy',
                ),
            ),
         )
     );
    
like image 2
edigu Avatar answered Nov 15 '22 13:11

edigu