Yii2 无法 运行 控制台应用程序
Yii2 unable to run console application
我正在尝试 运行 我在 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
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
'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();
}
}
更新 1
下面是我的组件代码
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;
}
我不知道主要问题是什么,因为我尝试检查问题但找不到解决方案。
我只能猜测(因为您没有显示您的配置)您的 console 应用程序配置了 response
组件,例如:
'components' => [
'response' => [
'formatters' => [/* some stuff here */],
// ...other settings
],
],
这是一个错误,很可能是由 copy-pasting Web 应用程序的配置引起的,这是有意义的,因为 Web 应用程序会自动连接 yii\web\Response
(其中有 formatters
选项可用)并且控制台正在使用 yii\console\Response
(没有 formatters
)。如果是这种情况 - 只需删除不必要的配置。如果您对 Web 和控制台使用相同的配置,则必须将其分开。
我正在尝试 运行 我在 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
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
'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();
}
}
更新 1
下面是我的组件代码
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;
}
我不知道主要问题是什么,因为我尝试检查问题但找不到解决方案。
我只能猜测(因为您没有显示您的配置)您的 console 应用程序配置了 response
组件,例如:
'components' => [
'response' => [
'formatters' => [/* some stuff here */],
// ...other settings
],
],
这是一个错误,很可能是由 copy-pasting Web 应用程序的配置引起的,这是有意义的,因为 Web 应用程序会自动连接 yii\web\Response
(其中有 formatters
选项可用)并且控制台正在使用 yii\console\Response
(没有 formatters
)。如果是这种情况 - 只需删除不必要的配置。如果您对 Web 和控制台使用相同的配置,则必须将其分开。