当前位置: 首页 > 知识库问答 >
问题:

yii2无法运行控制台应用程序

袁泓
2023-03-14

我试图在yii2中运行控制台应用程序,但出现错误。

异常“yii\base\UnknownPropertyException”,在F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Component中显示消息“设置未知属性:yii\console\Response::formatters”。菲律宾比索: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本地。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;
}

我不知道主要问题是什么,因为我试图检查问题,但找不到解决方案。

共有1个答案

林英锐
2023-03-14

我只能猜测(因为您没有显示配置),您的控制台应用程序配置了response组件,如:

'components' => [
    'response' => [
        'formatters' => [/* some stuff here */],
        // ...other settings
    ],
],

这是一个错误,最有可能是由复制粘贴配置的Web应用程序,这是有意义的,因为Web应用程序自动连接yii\web\响应(其中有格式化程序选项可用)和控制台使用yii\控制台\响应(它没有格式化程序)。如果是这种情况,只需删除不必要的配置。如果您对Web和控制台使用相同的配置,则必须将其分开。

 类似资料:
  • 我有一台服务器(Ubuntu 14.04.4 LTS),并希望某个Java应用程序一直在其上运行。目前我使用

  • 刚刚将一个应用程序从V12升级到V13-仅在帮助中进行文本更改。工作很好。 上传给开发人员。它不会发布新的APK,说: “无法发布此配置,原因如下:版本12未提供给任何设备配置:所有可能接收版本12的设备都将接收版本13。” 它在说什么?V12是针对40K+设备发布的,我刚刚对其他三个应用程序做了完全相同的事情,没有任何问题,但这一个不会有。 不知道这是什么意思。我能做的就是把它存为草稿。

  • 问题内容: 我已经开发了一个Java控制台应用程序,该应用程序在启动时打开控制台窗口并保持在前台,我想在后台启动该应用程序。 现在,我通过以下命令行启动该应用程序: 有没有办法实现这种行为?更改命令行参数就足够了,还是我需要对代码进行一些更改? 问题答案: 答案取决于操作系统。

  • 我有一个 .NET Core 控制台应用程序并运行了 。但是,我不知道如何从命令行运行应用程序。有什么提示吗?

  • 假设我有一个Java(或Kotlin,其实并不重要)应用程序,它使用Java。io。控制台API或任何其他操作终端状态的API(例如:): 当Maven Exec插件启动时,上述代码运行正常,但对于Gradle(因为Gradle试图使自己的输出看起来很漂亮,带有所有的铃铛、哨子和进度条),代码只打印: 配置任务,如下所述: 或者按照这里的建议将添加到Gradle的命令行参数并没有真正的帮助(仍然是