开发者

Zend & Doctrine2: Error with proxies from command line scripts

开发者 https://www.devze.com 2023-02-04 03:58 出处:网络
I\'m working with Zend and Doctrine2 and I needed to create some cronjobs using the same structure. the problem is, when I tried to get the repository from a Model with relationship with another Model

I'm working with Zend and Doctrine2 and I needed to create some cronjobs using the same structure. the problem is, when I tried to get the repository from a Model with relationship with another Model, I got an error (only in command-line, in the website works fine if I do the same).

This is the error I get in the command-line:

Fatal error: Class 'Proxy\Model_MediaPresetsProxy' not found in c:\php\library\Doctrine\ORM\Proxy\ProxyFactory.php on line 92

I have the proxies in this folder /application/models/proxies/ and the file Model_MediaPresetsProxy is in that directory

<?php

namespace Proxy;

/**
 * THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
 */
class Model_MediaPresetsProxy extends \Model_MediaPresets implements \Doctrine\ORM\Proxy\Proxy
{
[...]

Here is what I created for the crons

/crons/init.php

<?php

$time = microtime(true);
$memory = memory_get_usage();

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', 'cldev');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

defined('NL')
    || define('NL', "\n");

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();

// set bootstrap param 
$bootstrap = $application->getBootstrap(); 
$front = $bootstrap->getResource('FrontController');
$front->setParam('bootstrap', $bootstrap); 

register_shutdown_function('__shutdown');

function __shutdown()
{
    global $time, $memory;
    $endTime = microtime(true);
    $endMemory = memory_get_usage();

    echo '
    Time [' . ($endTime - $time) . '] Memory [' . number_format(( $endMemory - $memory) / 1024) . 'Kb]
';
}

and then I created my cronjob to test that everything was working

/crons/queue/processQueue.php

<?php 

require realpath( dirname(__FILE__) . '/../init.php');


$paths = Helper_Media::getPaths();
$magick = new App_PhMagick();

$em = Zend_Registry::getInstance()->entityManager;

$queue = $em->getRepository('Model_MediaQueue')->findAll();

echo 'Items in the queue: ' . count($queue) . NL;

And this are the models I'm using for that

/application/models/MediaQueue.php

<?php

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="media_queue")
 */
class Model_MediaQueue
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $queue_id;


    /** 
     * @ManyToOne(targetEntity="Model_MediaPresets")
     * @JoinColumn(name="queue_preset_id", referencedColumnName="preset_id")
     */
    private $media_preset;

    /** 
     * @ManyToOne(targetEntity="Model_Sites", inversedBy="media_list")
     * @JoinColumn(name="queue_site_id", referencedColumnName="site_id")
     */
    private $media_site;

    /** 
     * @Column(type="string")
     */
    private $source_media_file;

    /** @Column(type="integer") */
    private $result_media_id;

    /** @Column(type="integer") */
    private $queue_status;

    /** @Column(type="integer") */
    private $queue_added;

    /** @Column(type="integer") */
    private $queue_processed;


    public function __construct()
    {
        $this->media_preset = new \Doctrine\Common\Collections\ArrayCollection();
        $this->media_site = new \Doctrine\Common\Collections\ArrayCollection();
    }

    [...]
}

/application/models/MediaPresets.php

<?php

/**
 * @Entity
 * @Table(name="media_presets")
 */
class Model_MediaPresets
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $preset_id;

    /** 
     * @Column(name="preset_type", type="string", columnDefinition="enum('video', 'photo')") 
     */
    private $preset_type;

    /** @Column(type="string") */
    private $preset_name;

    /** @Column(type="string") */
    private $preset_formats_order;

    /** @Column(type="integer") */
    private $preset_size_w;

    /** @Column(type="integer") */
    private $preset_size_h;

    /** 
     * @Column(name="preset_resize_method", type="string", colu开发者_运维问答mnDefinition="enum('Maintain', 'Stretch', 'Centre', 'Abort')") 
     */
    private $preset_resize_method;

    /** @Column(type="string") */
    private $preset_watermark;

    /** @Column(type="string") */
    private $preset_background;

}

any clues?

Many thanks :)


Well after a while I found myself the solution.

The problem was that I created 2 different Environments: DEVELOPMENT and CLDEV (Command-line development)

and my getEntityManager() function had a line with

[...]
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
[...]

So I replaced it with

[...]
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development' || APPLICATION_ENV == 'cldev'));
[...]

and the problem is solved. Hope it will help somebody else in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消