Trying to create upload file but getting error Yii2

Hi everybody!

When i try to create payment it gives me an error: Trying to get property of non-object. Error in attachment Can anybody help? :unsure:


public function actionCreate()

    {

        $model = new Transactions();

        $users = ArrayHelper::map(Users::find()->all(), 'id', 'username');

        $services = ArrayHelper::map(Services::find()->all(), 'id', 'service_name');

        $arrWho = array();

        $arrWhoIds = array();

        if ($model->load(Yii::$app->request->post())) {


            $model->payment_file = UploadedFile::getInstance($model, 'payment_file');


            $image_name = $model->payment_title.rand(1, 4000).'.'.$model->payment_file->extension;


            $image_path = 'uploads/payments'.$image_name;


            $model->payment_file->saveAs($image_path);


            $model->payment_file = $image_path;



If you need me to provide the other data or any other information, just tell me.

Thanks in Advance.

Is important to know in with line are you getting the error so you can know which is the object that has not been instantiated.

Hi Ginna, welcome to the forum.

Since UploadedFile::getInstance() may return Null, "$model->payment_file->extension" can be "property of non-object".

You have to validate the model before you proceed to save the uploaded file.




public function actionCreate()

    {

        $model = new Transactions();

        ...

        if ($model->load(Yii::$app->request->post())) {

            $model->payment_file = UploadedFile::getInstance($model, 'payment_file');

            if ($model->validate()) {

                $image_name = $model->payment_title.rand(1, 4000).'.'.$model->payment_file->extension;

                ...



Guide > Uploading Files

http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html

This is the line where i get error: $image_name = $model->payment_title.rand(1, 4000).’.’.$model->payment_file->extension;

I thought i attached the screen with error.

Thanks for help.

Thanks for having me))

Now it gives me the blank page.

Thanks for help.

Hmm, too little information to give any advice. Could you post the code of your actionCreate() ?

Yes, sure.




public function actionCreate()

    {

        $model = new Transactions();

        $users = ArrayHelper::map(Users::find()->all(), 'id', 'username');

        $services = ArrayHelper::map(Services::find()->all(), 'id', 'service_name');

        $arrWho = array();

        $arrWhoIds = array();

        if ($model->load(Yii::$app->request->post())) {


            $model->payment_file = UploadedFile::getInstance($model, 'payment_file');


            if ($model->validate()) {


            $image_name = $model->payment_title.rand(1, 4000).'.'.$model->payment_file->extension;


            $image_path = 'uploads/payments/'.$image_name;


            $model->payment_file->saveAs($image_path);


            $model->payment_file = $image_path;}




            if(isset($model->who_has_to_pay_for_it_ids)){

                if(isset($model->who_has_to_pay_for_it_ids[0])){

                    $model->who_has_to_pay_for_it_ids = array_keys($users);

                    $arrWho = $users;

                    unset($arrWho[$model->who_paid_id]);

                    $arrWhoIds = array_keys($arrWho);


                } else if(count($model->who_has_to_pay_for_it_ids)){

                    $arrWhoIds = $model->who_has_to_pay_for_it_ids;

                    unset($arrWhoIds[$model->who_paid_id]);


                    foreach ($arrWhoIds as $value){

                        $arrWho[$value] = $users[$value];

                    }

                }

                $model->who_has_to_pay_for_it = implode(",", $arrWho);

                $model->who_has_to_pay_for_it_ids = implode(",", $arrWhoIds);

                $model->amount_per_person = $model->amount / (count($arrWhoIds)+1);

            }

            if ($model->save()){

                //var_dump($arrWhoIds); die();

                foreach ($arrWhoIds as $deptId){

                    $deptors = new Deptors();

                    $deptors->transaction_id = $model->id;

                    $deptors->deptor_user_id = $deptId;

                    $deptors->amount = $model->amount_per_person;

                    $deptors->debt = $model->amount_per_person;

                    $deptors->must_pay_to = $model->who_paid_id;

                    $deptors->save();

                }




                return $this->redirect(['view', 'id' => $model->id]);

            }

        } else {

            return $this->render('create', [

                'model' => $model,

                'users' => $users,

                'services' => $services

            ]);

        }

    }


    /**

     * Updates an existing Transactions model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @return mixed

     */



I added code tag to your previous post to make it easier for us to read. But it was not good enough. So I used my editor (PhpStorm) to reformat your code. Looks nice, doesn’t it? IMO it’s very important to use a good tool.

Anyway, you should notice that there’s a path in which the action ends without redirecting nor rendering a view. That’s why you get a blank page.

Thanks again for help.

Now i’m totally baffled)

What do you think i’m missing? Because seems like i don’t miss anything in my code. Any suggestions?

Note that when "$model->load(Yii::$app->request->post())" succeeds but "$model->save()" fails the code does nothing.

I’m not very sure but I guess your code should be something like the following:




public function actionCreate()

{

    $model = new Transactions();

    $users = ArrayHelper::map(Users::find()->all(), 'id', 'username');

    $services = ArrayHelper::map(Services::find()->all(), 'id', 'service_name');

    $arrWho = array();

    $arrWhoIds = array();

    if ($model->load(Yii::$app->request->post())) {

        $model->payment_file = UploadedFile::getInstance($model, 'payment_file');

        if ($model->validate()) {

            $image_name = $model->payment_title . rand(1, 4000) . '.' . $model->payment_file->extension;

            $image_path = 'uploads/payments/' . $image_name;

            $model->payment_file->saveAs($image_path);

            $model->payment_file = $image_path;

            if (isset($model->who_has_to_pay_for_it_ids)) {

                if (isset($model->who_has_to_pay_for_it_ids[0])) {

                    $model->who_has_to_pay_for_it_ids = array_keys($users);

                    $arrWho = $users;

                    unset($arrWho[$model->who_paid_id]);

                    $arrWhoIds = array_keys($arrWho);

                } else if (count($model->who_has_to_pay_for_it_ids)) {

                    $arrWhoIds = $model->who_has_to_pay_for_it_ids;

                    unset($arrWhoIds[$model->who_paid_id]);

                    foreach ($arrWhoIds as $value) {

                        $arrWho[$value] = $users[$value];

                    }

                }

                $model->who_has_to_pay_for_it = implode(",", $arrWho);

                $model->who_has_to_pay_for_it_ids = implode(",", $arrWhoIds);

                $model->amount_per_person = $model->amount / (count($arrWhoIds) + 1);

            }

            if ($model->save(false)) {

                //var_dump($arrWhoIds); die();

                foreach ($arrWhoIds as $deptId) {

                    $deptors = new Deptors();

                    $deptors->transaction_id = $model->id;

                    $deptors->deptor_user_id = $deptId;

                    $deptors->amount = $model->amount_per_person;

                    $deptors->debt = $model->amount_per_person;

                    $deptors->must_pay_to = $model->who_paid_id;

                    $deptors->save();

                }

                return $this->redirect(['view', 'id' => $model->id]);

            }

        }

    }

    return $this->render('create', [

        'model' => $model,

        'users' => $users,

        'services' => $services

    ]);

}



With this code i get an error: explode() expects parameter 2 to be string, array given.

use yii\helpers\Html;

use yii\widgets\ActiveForm;

/* @var $this yii\web\View */

/* @var $model app\models\Transactions */

/* @var $form yii\widgets\ActiveForm */

error line -> $whoHasToPay = explode(",", $model->who_has_to_pay_for_it_ids);

$whoHasToPay = array_combine($whoHasToPay, $whoHasToPay);

?>

<div class="transactions-form">

&lt;?php &#036;form = ActiveForm::begin([


         'options' =&gt; ['enctype' =&gt; 'multipart/form_data']


    ]); ?&gt;


&lt;?= &#036;form-&gt;field(&#036;model, 'who_paid_id')-&gt;dropDownList(&#036;users)-&gt;label('Who paid') ?

So, you are setting an array to $model->who_has_to_pay_for_it_ids somewhere in your code.

I’m sorry but I couldn’t find where it is.