PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] One line way to find an attribute - Yii

 January 28, 2022     php, yii     No comments   

Issue

I've recently been using the controller to pass in a couple of values into my view. In my controller this is what it looks like when I pass in values from the controller to the view.

public function actionProcessPayment($id)
{
    // if the account id was passed in 
    if(isset($_POST['listF']))
    {
        $account_id = $_POST['listF'];
        // total due before payment 
        $totaldue = Recipient::model()->totaldue($id);
        $result = Recipient::model()->MakePayment($id, $account_id); 

        if($result == 'not enough money')
        {
            Yii::app()->user->setFlash('not enough money', "This account does not have enough money 
                to make that transaction!");
            $this->redirect(array('recipient/index', 'id' => $id)); 
        }
        else 
        $this->render('paysummary',array(
            'id'=>$id,
            'totaldue'=>$totaldue,
            'numpeople'=>Paylist::model()->NumIndv($id),
            'user_id'=>Login::model()->getUserId(),
            'accountname'=>Account::model()->AccountName($account_id),
            'accountbalance'=>Account::model()->AccountBalance($account_id), 
            ''

        ));
    }

Now, in order to get the account name (for example), I've created a function in the model called AccountName that takes in the account id as a parameter.

Like so

    public function AccountName($id)
    {
        $model = Account::model()->findByPk($id);
        return $name = $model->name;
    }

This is working fine, but I feel like I am programming in a very roundabout way. Is there a way to change this to be one line of code? Is this how it should be called in the controller?


Solution

public function actionProcessPayment($id)
{
    // if the account id was passed in 
    if(isset($_POST['listF']))
    {
        $account_id = $_POST['listF'];
        // total due before payment 
        $totaldue = Recipient::model()->totaldue($id);
        $result = Recipient::model()->MakePayment($id, $account_id); 
        $model = Account::model()->findByPk( $account_id );// <<---

        if($result == 'not enough money')
        {
            Yii::app()->user->setFlash('not enough money', "This account does not have enough money 
                to make that transaction!");
            $this->redirect(array('recipient/index', 'id' => $id)); 
        }
        else 
        $this->render('paysummary',array(
            'id'=>$id,
            'totaldue'=>$totaldue,
            'numpeople'=>Paylist::model()->NumIndv($id),
            'user_id'=>Login::model()->getUserId(),
            'accountname'=>$model->name,// <<---
            'accountbalance'=>$model->balance, // <<---
            ''

        ));
    }


Answered By - Developerium
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing