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

时间:2014-08-02 14:05:36

标签: php yii

我正在考虑为记录事务结果的数据表实例化模型的最佳位置。数据表仅在外围与视图交互(视图指示选择哪个“帐户”将支付交易)。但是,除了这些数据之外,所有其他数据都可以在模型的业务端找到。我知道在Yii中,生成代码中实例化模型的唯一时间是在控制器中的actionCreate中 - 但是如果要在数据库中添加新列,那么你不能在模型中轻松完成吗? / p>

这是模型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";

    }

我的问题是在我的模型Recipient.php中实例化这两个模型是否合适。这一切似乎都运行良好,并且它使我的控制器相对精简 - 但我想确保这是实例化模型的适当方式。

0 个答案:

没有答案