Friday, 10 October 2014

How to set Cron in Zend Framework?

How to set Cron in Zend Framework?

Step 1: Don't execute bootstrap's run function directly in index.php
File Location: public_html/index.php
Update Following code:
if (php_sapi_name() != 'cli' || !empty($_SERVER['REMOTE_ADDR'])) {
 $application->bootstrap()->run();
}


Step 2: Create a folder cronjob in web application root folder.


Step 3. Create a file init.php under cronjob folder and add following code.
// Define path to application directory
defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

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

// 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();



Step 4: Create a file mycron.php under cronjob folder.
Add following code in mycron.php
require_once 'init.php';
print_r($_SERVER);


Step 5. Append following code in mycron.php.
Now, you can access all models like below
$obj = new Application_Model_Logs();
In this file you can access zend's all global variable even you can access all models files with same way you had used in controller.

Step 6: Now Test the cron with following command
Login to putty and go to folder where your PHP is installed and run following code.
php C:\wamp\www\myproject\cronjob\mycron.php 




Thursday, 9 October 2014

Calculate a Sum of Time using MySQL

Calculate a Sum of Time using MySQL

We have Following Table Structure of myvideos

CREATE TABLE IF NOT EXISTS `myvideos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `time` time NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='3' AUTO_INCREMENT=5 ;


INSERT INTO `myvideos` (`id`, `name`, `time`) VALUES
(1, 'Video 1', '02:02:00'),
(2, 'Video  2', '02:02:00'),
(3, 'Video  3', '02:02:00'),
(4, 'Video  4', '02:04:00');


Problem: How to calculate the sum of time of video?


Solution:
select sec_to_time(sum(time_to_sec(time))) as total_time from myvideos