Yii Framework Forum: Creating A Cron Based Console Application - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Creating A Cron Based Console Application Rate Topic: -----

#1 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 29 April 2013 - 07:32 PM

I am having trouble debugging the console application, there seems to be some differences between some examples and the wiki
entries for console apps.

One point of confusion is whether to use "yiic test" or "cron.php test"

I created a simple command file extending CConsoleCommand
TestCommand.php:
<?php 
class TestCommand extends CConsoleCommand {
  
    public function run($args) {
        // here we are doing what we need to do

	// create a contact record
	$c = new Contact;
	$c->last_name = 'Harter';
	$c->first_name = 'Bryan';
	$c->contact_type = 2; 
	$c->save();

    }
}
?>



cron.php script file:
#!/usr/bin/env php
<?php
// defined('YII_DEBUG') or define('YII_DEBUG',true);

// including Yii
require_once(dirname(__FILE__).'/../framework/yii.php');
 
// we'll use a separate config file
$config=dirname(__FILE__).'/config/cron.php';
 
// creating and running console application
Yii::createConsoleApplication($config)->run();
?>


Cronjob file cron.php:
<?php
return array(
    // This path may be different. You can probably get it from `config/main.php`.
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'Cron',

    'preload'=>array('log'),

    'import'=>array(
	'application.components.*',
	'application.models.*',
    ),
    // We'll log cron messages to the separate files
    'components'=>array(
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'logFile'=>'cron.log',
                    'levels'=>'error, warning',
                ),
                array(
                    'class'=>'CFileLogRoute',
                    'logFile'=>'cron_trace.log',
                    'levels'=>'trace',
                ),
            ),
	),

        // DB connection
	'db'=>array(
		'class' => 'CDbConnection',
		'connectionString' => 'mysql:host=localhost;dbname=***',
		'emulatePrepare' => true,
		'username' => '*******',
		'password' => '*******',
		'charset' => 'utf8',
        ),
    ), //components
);
?>


I call the script from the protected folder ./cron.php test

I receive output of PHP Error[2]: include (EActiveRecordRelationBehavior.php): filed to open stream: No such file or directory
in file /usr/share/nginx/www/myapp/framework/YiiBase.php at line 421.

Seems to be an autoload problem from YiiBase.php

It seemed to find the model and other components, but I am uncertain what the problem is.

Anyone have any ideas what I am doing wrong or need to change?

Thanks!
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 30 April 2013 - 02:02 AM

in your contact model behaviors method you have to write full path to your EActiveRecordRelationBehavior class,

...
'class' => 'ext.behaviors.EActiveRecordRelationBehavior'
...

0

#3 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 30 April 2013 - 08:42 AM

Thank you MBI I will make that change and continue testing.
0

#4 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 30 April 2013 - 09:13 AM

It made a difference but still errored out.
'CException' with message 'Alias "ext.behaviors.EActiveRecordRelationBehavior" is invalid. Make sure it points to an existing PHP file and file is readable.' in /usr/share...YiiBase.php:316

My EActiveRecordRelationBehavior file should not be anything out of the ordinary...I've been using the contact model
as a standard model for several months.

ext.behaviours...would imply it is an extension of the AR behaviors....but it is plain vanilla yii...

I am however using awegen for my generator in place of standard gii.

I had to change the path to 'ext.awegen.components...'

Thanks so much for the help!!
0

#5 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 30 April 2013 - 09:16 AM

Is it possible to call a controller method ?

I was successful in doing a simple data insert using Active Record methods.

Normally the controller renders a view. Do I need to create a new method that just
does what I need it to do?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users