Still struggling to get doctrine working properly on my system. I have a yaml file that describes the structure of my database and tables. (I am following the tutorial on zendcast). I have my doctrine script file as
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
include('doctrine.php');
and the doctrine.php file contains
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
My application.ini excerpt is
autoloaderNamespaces[] = "Doctrine_"
;--
;Database
;--
doctrine.dsn = "mysql://root:softna#localhost/gepm2"
doctrine.data_fixtures_path = APPLICATION_PATH "/configs/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/configs/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/configs/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/configs/schema.yml"
doctrine.models_path = APPLICATION_PATH "/../library/Gepm/Model"
When I run:
D:\www\gepm2\application\scripts>php doctrine build-all-reload
I get the ff feedback:
D:\www\gepm2\application\scripts>php doctrine build-all-reload
build-all-reload - Are you sure you wish to drop your databases? (y/n) y
build-all-reload - Successfully dropped database for connection named 'doctrine'
build-all-reload - Successfully created database for connection named 'doctrine'
build-all-reload - Created tables successfully
build-all-reload - Data was successfully loaded
However when I check mysql only the database is created no tables are generated. I have struggled with this for a whole day. Someone help.
EDIT
All right I got it working now.But to be honest with you the only changes I made to the code was to
1. set the time zone,
2. remove the underscore from the end of the utoloaderNamespaces[] value so utoloaderNamespaces[] = "Doctrine_" became autoloaderNamespaces[] = "Doctrine" and
3. move the doctrine database declarations to the bottom of the [production] section in the applications.ini
I am so far behind on schedule with what I am working on that I cannot mess about with these changes to see which one or why it worked. I will do say later when I have some down time. In the meantime a more knowledgeable geek may be able to explain why it may have worked. cheers.
The way doctrine creates tables in the db is by loading the models and reading the definitions within each of these models. If the CLI is not able to load the models then the table creation will fail. Unfortunately the CLI task does not report this.
Further I would advise you to check other CLI tasks such as generate-migrations-diff and migrate. I have had issues with these in the past and have resolved them successfully.
Related
Classes managed through composer do not exist at the point I'm trying to instantiate them, which is in the controller.
I have my Zendframework 1.12.8 file/directory structure like this:
project
- application
- public
- index.php
- library
- vendor
- composer.json
- composer.lock
The contents of my index.php file are:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Composer autoloader */
if (file_exists(realpath(APPLICATION_PATH . '/../vendor/autoload.php'))) {
require_once realpath(APPLICATION_PATH . '/../vendor/autoload.php');
}
/** 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()
->run();
The composer autoload.php is included, but seems to be ignored or overwritten by Zend_Application. According to blogs I've seen this is the correct place to include it. Any clues?
Right. A colleague found the solutions: we need to prefix the namespace that composer has used when we instantiate the composer-managed class. So new Vimeo\Vimeo($foo, $bar) works, or putting use Vimeo\Video before making an instance new Vimeo($foo, $bar).
my index file is this
<?php
ini_set( "short_open_tag", 1 );
ini_set( "display_errors", 1 );
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
// Let 'er rip
$application->bootstrap()->run();
when i run that
Warning: require_once(Zend/Application.php): failed to open stream: No such file or directory in /var/www/Giftercity_backup/index.php on line 18 Fatal error: require_once(): Failed opening required 'Zend/Application.php' (include_path=':.:/usr/share/php:/usr/share/pear') in /var/www/Giftercity_backup/index.php on line 18
For Ubuntu.
If you install ZendFramework package. i.e. sudo apt-get install zend-framework
It will put zend library files in /usr/share/php/libzend-framework-php/Zend/ folder
you have to just copy and paste that Zend/ folder into /user/share/php/
Run the following command in terminal.
cd /usr/share/php
sudo cp -R libzend-framework-php/Zend/ Zend
Put your index file in "public" directory.
Or if you want or cannot include files from parent directory you need to change this line:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
To
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/library'), // Here we have change
get_include_path(),
)));
Of course I assume that you have already putted your Zend Files into library/Zend
You also need to remember to put .htaccess file with "deny from all" to your application, library, and any other directory you don't want users to get access to.
Btw.
This method of including library is quite old and not recommended.
If your index.php file is located in /var/www/Giftercity_backup/index.php (according to the error output) then according to your code the library location should be in /var/www/library and application in /var/www/application which sounds wrong.
index.php should be in a public folder such as /var/www/Giftercity_backup/public, /var/www/Giftercity_backup/htdocs or similar and your VirtualHost DocumentRoot should point to that folder.
Additionally, your set_include_path didn't register library (include_path=':.:/usr/share/php:/usr/share/pear').
I am trying to find the best way to implement Model using Zend Framework for an enterprise application. From different articles I am now convinced that a Service Layer is a very good idea. I see that one of the arguments in favor of Service Layer is that - it can be called from outside - like from crons, SOAP, command line tasks and Queues.
But I am not clear how it can do so. When services are called from outside the Bootstrap will not run hence the model will have no information about the DB, Mail Transport, Logging etc.
Any suggestions?
The simplest way is to create a CLI script which is used to do your cron task.
You can bootstrap your application in the CLI script, just like it gets bootstrapped in the web end of things, using Zend_Application and the bootstrap class.
Just don't run the application, instead only bootstrap it. This way you will have access to the same environment as your web app has.
We're using a simple init.inc.php script that we include in our command line scripts and cronjob scripts, which bootstrap the resources that we need:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application') );
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
// we can't afford not have a APPLICATION_ENV, so return a fatal error in this case
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: ''));
chdir(APPLICATION_PATH);
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$resources = array('autoload', 'config', 'multidb', 'logger', 'cache', 'settings');
foreach ($resources as $resource) {
$application->bootstrap($resource);
}
set_time_limit(1200);
ini_set('memory_limit', '700M');
the $resources array is the bootstrap functions you wish to load
the APPLICATION_ENV is usually a variable set by .htaccess, so you'll have to set it a shell variable (or just include it in the init.inc.php)
How run zend framework action (inside index controller) by cron every 12 hours?
The case:
I have basic(no modules) zend project (1.11) that created by zf tool.
Inside main IndexController exist cronAction() - url http://mydomain/index/cron.
Need to run cronAction() once per 12 hours by cron.
Thanks
Find the crontab file and add this line:
0 0,12 * * * curl --silent --compressed http://mydomain/index/cron
You can also do it with other tools, such as lynx or wget, not necassarily curl - the above is just an example.
I know I am bit late but I would like to leave another solution, maybe it help other people, you could run the file in cron if you have your business rule inside model
By creating a file in the public folder with the content below. Ex.: cron.php
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** 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();
$model = new Application_Model_Name();
$model->runTask();
Then add a cron tab entry
0 0,12 * * * php /path/to/your/project/cron.php
It should work better than first answer since you will run using PHP CLI then you won't have execution time limit of php script, in case of your script takes more than one minute and you don't need network connection to run that cron job
In Zend Framework 2 You can run a cron job using console routes. Take a look at the example posted here: http://collabedit.com/58v4v
I'm following the tutorial on setting up WURFL with Zend Framework to enable easy mobile browser detection.
http://framework.zend.com/manual/en/zend.http.user-agent.html#zend.http.user-agent.quick-start
I have got it setup to the point where it can detect a desktop browser and give me all the details and features of that browser, but when I try to access the website using an iPhone (mobile safari) it throws an error when trying to write to the cache directory.
Here's what I'm seeing in my error logs:
2011-06-08T22:32:34-07:00 ERR (3): The file cache directory does not exist and could not be created. Please make sure the cache directory is writeable: /var/tmp
However in my configuration at /application/configs/wurfl-config.php I have set the cache directory to the following:
<?php
$resourcesDir = dirname(__FILE__) . '/../../data/wurfl/';
$wurfl['main-file'] = $resourcesDir . 'wurfl-2.0.27.zip';
$wurfl['patches'] = array($resourcesDir . 'web_browsers_patch.xml');
$persistence['provider'] = 'file';
$persistence['dir'] = $resourcesDir . '/cache/';
$cache['provider'] = null;
$configuration['wurfl'] = $wurfl;
$configuration['persistence'] = $persistence;
$configuration['cache'] = $cache;
I've also ensured it is writable by the server, but wurfl seems to think my cache directory is still /var/tmp
How can I get wurfl to observe my cache directory setting?
Notes: The tutorial uses wurfl-1.1 as the example, I have only been able to find wurfl-1.3 on sourceforge. This may be an issue.
Notes: I have these lines in my application.ini file:
; WURFL
resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/wurfl-php-1.3.0/WURFL/"
resources.useragent.wurflapi.wurfl_config_file = APPLICATION_PATH "/configs/wurfl-config.php"
Not sure if this is the correct way to fix it, but for me the issue was solved by adding an extra .dir after the persistence.dir key (using WURFL 1.3.0):
In application.ini: (I don't use the php config file as I prefer not to mix in php code if I can use .ini directives)
resources.useragent.wurflapi.wurfl_config_array.persistence.dir.dir = APPLICATION_PATH "/../data/wurfl/cache/"
So my complete config for WURFL looks like this in Zend's application.ini:
; Mobile device detection
resources.useragent.storage.adapter = "Session"
resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/WURFL/"
resources.useragent.wurflapi.wurfl_config_array.wurfl.main-file = APPLICATION_PATH "/../data/wurfl/wurfl.xml"
resources.useragent.wurflapi.wurfl_config_array.wurfl.patches[] = APPLICATION_PATH "/../data/wurfl/web_browsers_patch.xml"
resources.useragent.wurflapi.wurfl_config_array.persistence.provider = "file"
resources.useragent.wurflapi.wurfl_config_array.persistence.dir.dir = APPLICATION_PATH "/../data/wurfl/cache/"
perhaps a bug in the framework regarding how it reads the config array it's being passed?
I just resolved the problem ;)
remove the [] from the code line below:
resources.useragent.wurflapi.wurfl_config_array.wurfl.patches[] = APPLICATION_PATH "/../data/wurfl/web_browsers_patch.xml"
transform it to:
resources.useragent.wurflapi.wurfl_config_array.wurfl.patches = APPLICATION_PATH "/../data/wurfl/web_browsers_patch.xml"
It seems the format of the parameters has changed in version 1.3 - the WURFL docs here have the details and an example file.
So for the original question, the $persistence['dir'] line needs to be changed to:
$persistence['params'] = array(
'dir' => $resourcesDir . '/cache/'
);
I solved the problem using Wurfl 1.3.1 and reading this:
http://wurfl.sourceforge.net/nphp/
With regards to Jens Wegar's answer above, there is a bug-fix request in to fix this as it's not clear.
http://framework.zend.com/issues/browse/ZF-12284
Did you configure the UserAgent resource to use the settings you are showing here?
You have to add resource.useragent.wurfl_* entries into your application.ini file.
Here is a sample:
resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/WURFL/"
resources.useragent.wurflapi.wurfl_config_file = APPLICATION_PATH "/configs/wurfl-config.php"