Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
您应该在控制器或模型中实例化模型吗?-Yii,PHP_Php_Yii - Fatal编程技术网

您应该在控制器或模型中实例化模型吗?-Yii,PHP

您应该在控制器或模型中实例化模型吗?-Yii,PHP,php,yii,Php,Yii,我正在尝试为记录事务结果的datatable实例化模型的最佳位置。datatable仅与视图进行外围交互(视图指示选择哪个“帐户”来支付交易)。但是,除了这些数据之外,所有其他数据都可以在模型的业务端找到。我知道在Yii中,您在生成的代码中实例化模型的唯一时间是在控制器中的actionCreate中,但是如果您想向数据库中添加一个新列,您不能在您的模型中同样轻松地执行吗 下面是Recipient.php模型中的processpayment——它调用服务器并返回结果。然后将结果保存到数据库中

我正在尝试为记录事务结果的datatable实例化模型的最佳位置。datatable仅与视图进行外围交互(视图指示选择哪个“帐户”来支付交易)。但是,除了这些数据之外,所有其他数据都可以在模型的业务端找到。我知道在Yii中,您在生成的代码中实例化模型的唯一时间是在控制器中的actionCreate中,但是如果您想向数据库中添加一个新列,您不能在您的模型中同样轻松地执行吗

下面是Recipient.php模型中的processpayment——它调用服务器并返回结果。然后将结果保存到数据库中

   /** 
    * 
    * MakePayment: this function helps us pay the recipients defined by a certain paylist 
    * @param $account_id identifies the mobile-money account that will pay the recipients 
    * @param $list_id is the id of the paylist that is going to be paid. 
    * @return if 
    */
    function MakePayment($account_id, $list_id)
    {
        // instantiates new column in BulkPayment table 
        $BPModel = new BulkPayment;
        // inserts the new BPModel into the database 
        $BPModel->save();

        // instantiate the account that is being used to pay money
        $account = Account::model()->findByPk($account_id);

        // finds all individuals of the selected list
        $recipients = Recipient::model()->findAll(array("condition"=>"list_id = $list_id"));

        // loop through the array of recipients, paying each of the individuals 
        foreach($recipients as $recipient)
        {
            // instantiates a new IndividualPayment
            $IPModel = new IndividualPayment;

            // Makes the transfer and returns the status of the transfer 
            $result = Recipient::model()->TransferMoney($account->msisdn, $account->pin, $recipient->msisdn, $recipient->balance);

            // defines the attributes that will be put into the 
            $attributes = array("name"=>$recipient->name, "recipient_id"=>$recipient->id, 
                            "transfer_id"=>$BPModel->id, "amount"=>$recipient->balance, "status"=>$result);
            // set the attributes of the Individual Payment 
            $IPModel->setAttributes($attributes, $safeOnly = false); 
            // save the new transaction into the database
            $IPModel->save();
        }
        // return "success";

    }

我的问题是在model Recipient.php中实例化这两个模型是否合适。这一切看起来都很好,并且使我的控制器相对精简,但我想确保这是实例化模型的适当方式

“模型”并不存在。模型层由各种组件组成,包括业务对象和服务。控制器调用服务方法,这些服务进行任何复杂的实例化。在其他模型方法中需要其他模型吗?在那里创建它。这比在控制器中创建并传递所有内容更直接。它也会炸毁你的控制器。好吧,只是改变了我的实现。查看一下,看看您是否认为这是合适的,
$BPModel
用于什么?你创建了它,然后马上保存,但它是空的?还有一件事,如果涉及资金转移,你可能想调查一下以避免麻烦。