How can I access Yii models outside of the framework?
I have some gearman workers performing tasks and are managed using BrianMoons GearmanManager. I'd like to be able to access a few Yii models in the worker's script without have to load the whole Yii framework. What do I need to import to load the models in my script? (CActiverecord, DBconnection, etc).
A worker looks like this:
as simple function
function reverse_string($job, &$log) {
$workload = $job->workload();
$result = strrev($workload);
$log[] = "Success";
return $result;
}
?>
or as a class:
<?php
class Sum {
private $cache = array();
private $foo = 0;
public function run($job, &$log) {
$workload = $job->workload();
if(empty($this->cache[$workload])){
$dat = json_decode($workload, true);
$sum = 0;
foreach($dat as $d){
$sum+=$d;
sleep(1);
}
$this->cache[$workload] = $sum + 0;
} else {
$sum = $this->cache[$workload] + 0;
}
$log[] = "Answer: ".$sum;
$this->foo = 1;
return $sum;
}
}
?>
I'd like to be able to access a few models and perform operations within the worker like so:
$foo=Foo::model()->findByPk($id);
$foo->attribute="bar";
$foo->save();