[英]Yii2 unable to run console application
I am trying to run my console application in yii2
but I am getting error.我正在尝试在
yii2
中运行我的控制台应用程序,但出现错误。
Exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: yii\console\Response::formatters' in F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Component.php:209
异常 'yii\base\UnknownPropertyException' 并在 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Component.php:209 中显示消息 'Setting unknown property: yii\console\Response::formatters'
Stack trace:
#0 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\BaseYii.php(558): yii\base\Component->__set('formatters', Array)
#1 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\BaseObject.php(107): yii\BaseYii::configure(Object(yii\console\Response), Array)
#2 [internal function]: yii\base\BaseObject->__construct(Array)
#3 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(400): ReflectionClass->newInstanceArgs(Array)
#4 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(159): yii\di\Container->build('yii\\console\\Res...', Array, Array)
#5 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\BaseYii.php(365): yii\di\Container->get('yii\\console\\Res...', Array, Array)
#6 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\ServiceLocator.php(137): yii\BaseYii::createObject(Array)
#7 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(742): yii\di\ServiceLocator->get('response', true)
#8 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Instance.php(164): yii\base\Module->get('response')
#9 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Instance.php(137): yii\di\Instance->get(NULL)
#10 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Controller.php(109): yii\di\Instance::ensure(Object(yii\di\Instance), 'yii\\base\\Respon...')
#11 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\BaseObject.php(109): yii\base\Controller->init()
#12 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Controller.php(98): yii\base\BaseObject->__construct(Array)
#13 [internal function]: yii\base\Controller->__construct('cron', Object(yii\console\Application), Array)
#14 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(392): ReflectionClass->newInstanceArgs(Array)
#15 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(159): yii\di\Container->build('console\\control...', Array, Array)
#16 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\BaseYii.php(345): yii\di\Container->get('console\\control...', Array)
#17 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(642): yii\BaseYii::createObject('console\\control...', Array)
#18 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(596): yii\base\Module->createControllerByID('cron')
#19 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(522): yii\base\Module->createController('monthlybill')
#20 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('cron/monthlybil...', Array)
#21 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('cron/monthlybil...', Array)
#22 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#23 F:\xampp\htdocs\mdc\yii(27): yii\base\Application->run()
#24 {main}
Press any key to continue . . .
Common\config\main-local.php Common\config\main-local.php
'components' => [
'mycomponent' => [
'class' => 'common\components\MyComponent',
],
'response' => [
'formatters' => [
'pdf' => [
'class' => 'robregonm\pdf\PdfResponseFormatter',
],
]
],
],
CronController定时控制器
namespace console\controllers;
use yii\console\Controller;
use Yii;
use yii\filters\AccessControl;
class CronController extends Controller {
public function actionMonthlybill()
{
Yii::$app->mycomponent->billing();
}
}
Update 1更新 1
Below is my component code下面是我的组件代码
public static function billing()
{
$date = date('Y-m-d 23:59:59');;
$rel = MdcmetercustRel::find()->all();
if(count($rel)>0)
{
foreach ($rel as $item) {
$tariff_id = $item->tariff_id;
$tariff_name = $item->tariff_name;
$consumer_no = $item->cust_id;
$consumer_name = $item->name;
$msn = $item->msn;
$meter_type = $item->meter_type;
$consumer_address = $item->cust_address;
$prev_read = 0;
$p_data = \Yii::$app->db->createCommand(/** @lang text */ "SELECT m.`curr_read` FROM `mdc_bill_details`
m WHERE m.`consumer_no` = '$consumer_no' ORDER BY m.`id` DESC LIMIT 1;")->queryOne();
if ($p_data)
{
$prev_read = $p_data['curr_read'];
}
else
{
$prev_read = 0;
}
$mData = \Yii::$app->db->createCommand(/** @lang text */ "SELECT s.`slab_start`, s.`slab_end`, s.`rate` FROM `mdc_tariff_slabs` s
INNER JOIN `mdc_meter_cust_rel` r ON s.`t_id` = r.`tariff_id`
WHERE r.`cust_id` = $consumer_no")->queryAll();
$array = ArrayHelper::map($mData, 'slab_end', 'rate');
$data = MdcmetersData::find()->where(['and',['cust_id'=>$consumer_no],['<','data_date_time',$date]])->orderBy(['id' => SORT_DESC])->one();
$current_read = $data['kwh_t'];
$slabs = [100 => 10, 150 => 12, PHP_INT_MAX => 14];
$bill = self::billCalc($current_read,$slabs);
//echo substr ($x, 0, 3); // outputs 123
$read_diff = $current_read - $prev_read;
$avg_units = 0;
$bill_month = date('Y-m-d h:i:s');
$m_read_date = date('Y-m-d h:i:s');
$issue_date = date('Y-m-d h:i:s');
$due_date = date('Y-m-d h:i:s', strtotime('+ 8 days'));
$e_charges = $bill;
$e_arrears = 0;
$i_tax = 0;
$gst = 0;
$e_tax = 0;
$f_tax = 0;
$r_g_s_t = 0;
$e_total_amt = $e_charges + $e_arrears + $i_tax + $gst + $e_tax + $f_tax + $r_g_s_t;
$e_amt_w_due_date = $e_total_amt;
$e_amt_a_due_date = $e_amt_w_due_date + 300;
$c_duty = 0;
$rd_maint = 0;
$mosque = 0;
$conserve = 0;
$sewerage = 0;
$misc = 0;
$sanitation = 0;
$tv_fee = 0;
$rent = 0;
$rent_arrear = 0;
$ot_amt_w_due_date = $c_duty + $mosque + $rd_maint
+ $conserve + $sewerage + $misc + $sanitation + $tv_fee + $rent + $rent_arrear;
$ot_amt_a_due_date = $ot_amt_w_due_date;
$f_mrc = 0;
$f_mrc_arrear = 0;
$water_charge = 0;
$water_arrear = 0;
$wat_total_amt = $f_mrc + $f_mrc_arrear + $water_charge ;
$wat_amt_w_due_date =$wat_total_amt;
$wat_amt_a_due_date = $wat_amt_w_due_date + 10;
$g_total_w_due_date = $e_amt_w_due_date + $ot_amt_w_due_date +$wat_amt_w_due_date;
$g_total_a_due_date = $e_amt_a_due_date + $ot_amt_a_due_date + $wat_amt_a_due_date;
$m = new MdcBillDetails;
$m->t_id = $tariff_id;
$m->t_name = $tariff_name;
$m->consumer_no = $consumer_no;
$m->consumer_name = $consumer_name;
$m->consumer_address = $consumer_address;
$m->meter_no = $msn;
$m->meter_type =$meter_type;
$m->prev_read = $prev_read;
$m->curr_read = $current_read;
$m->read_diff = $read_diff;
$m->avg_units = $avg_units;
$m->total_units = $avg_units + $read_diff;
$m->bill_month = $bill_month;
$m->m_r_date = $m_read_date;
$m->issue_date = $issue_date;
$m->due_date = $due_date;
$m->e_charges = $e_charges;
$m->e_arrears = $e_arrears;
$m->i_tax = $i_tax;
$m->gst = $gst;
$m->e_tax = $e_tax;
$m->f_tax = $f_tax;
$m->r_g_s_t = $r_g_s_t;
$m->e_total_amt = $e_total_amt;
$m->e_amt_w_due_date = $e_amt_w_due_date;
$m->e_amt_a_due_date = $e_amt_a_due_date;
$m->c_duty = $c_duty;
$m->rd_maint = $rd_maint;
$m->mosque = $mosque;
$m->conserve = $conserve;
$m->sewerage = $sewerage;
$m->misc = $misc;
$m->sanitation = $sanitation;
$m->tv_fee = $tv_fee;
$m->rent = $rent;
$m->rent_arrear = $rent_arrear;
$m->ot_amt_w_due_date = $ot_amt_w_due_date;
$m->ot_amt_a_due_date = $ot_amt_a_due_date;
$m->f_mrc = $f_mrc;
$m->f_mrc_arrear = $f_mrc_arrear;
$m->water_charge = $water_charge;
$m->water_arrear = $water_arrear;
$m->wat_total_amt = $wat_total_amt;
$m->wat_amt_w_due_date = $wat_amt_w_due_date;
$m->wat_amt_a_due_date = $wat_amt_a_due_date;
$m->g_total_w_due_date = $g_total_w_due_date;
$m->g_total_a_due_date = $g_total_a_due_date;
if($m->save(false))
{
$hist = new MdcBillHist;
$hist->month_year = date('Y-m-d h:i:s');
$hist->units = $current_read;
$hist->bill_amt = $bill;
$hist->payment = 0;
$hist->customer_id = $consumer_no;
$hist->save(false);
}
}
}
else{
print_r('no data available');
exit();
}
}
public static function billCalc($input, $slabs)
{
$result = [];
$bill = 0;
$previous_slab = 0;
foreach($slabs as $slab => $rate)
{
if($slab=='')
$slab = PHP_INT_MAX;
// calculate distance between current and previous slab
$slab_distance = $slab - $previous_slab;
// if current remainder of input value is >= distance, add distance to result,
// and subtract distance from remainder of input
if( $input >= $slab_distance )
{
$result[] = $slab_distance;
$bill += $slab_distance * $rate;
$input -= $slab_distance;
}// otherwise, add remainder as last item of result, and break out of the loop here
else
{
$result[] = $input;
$bill += $input * $rate;
break;
}
$previous_slab = $slab;
}
return $bill;
}
I don't know what is the main issue as I have tried to check the issue but couldn't find the solution.我不知道主要问题是什么,因为我试图检查问题但找不到解决方案。
I can only guess (because you are not showing your configuration) that you have your console application configured with response
component like:我只能猜测(因为您没有显示配置)您的控制台应用程序配置了
response
组件,例如:
'components' => [
'response' => [
'formatters' => [/* some stuff here */],
// ...other settings
],
],
This is a mistake, most probably caused by copy-pasting the config for web application where this makes sense because web applications automatically wire yii\web\Response
(which has formatters
option available) and console is using yii\console\Response
(which doesn't have formatters
).这是一个错误,很可能是由于复制粘贴 web 应用程序的配置造成的,这是有道理的,因为 web 应用程序会自动连接 yii
yii\web\Response
(它有可用的formatters
选项)并且控制台正在使用yii\console\Response
(它不'没有formatters
)。 If this is the case - just remove the unnecessary configuration.如果是这种情况 - 只需删除不必要的配置。 If you are using the same config for both web and console you must separate it.
如果您对 web 和控制台使用相同的配置,则必须将其分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.