Ginna
(Solomonrondon77)
February 24, 2018, 10:41pm
1
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?
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.
menapro
(Medina)
February 25, 2018, 12:28am
2
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.
softark
(Softark)
February 25, 2018, 1:57am
3
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
Ginna
(Solomonrondon77)
February 25, 2018, 12:24pm
4
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.
Ginna
(Solomonrondon77)
February 25, 2018, 12:25pm
5
softark:
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
Thanks for having me))
Now it gives me the blank page.
Thanks for help.
softark
(Softark)
February 26, 2018, 10:46am
6
Hmm, too little information to give any advice. Could you post the code of your actionCreate() ?
Ginna
(Solomonrondon77)
February 26, 2018, 12:24pm
7
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
*/
softark
(Softark)
February 26, 2018, 1:11pm
8
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
]);
}
}
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.
Ginna
(Solomonrondon77)
February 26, 2018, 1:51pm
9
softark:
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?
softark
(Softark)
February 26, 2018, 2:23pm
10
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
]);
}
Ginna
(Solomonrondon77)
February 26, 2018, 3:50pm
11
softark:
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
]);
}
[/php]
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">
<?php $form = ActiveForm::begin([
'options' => ['enctype' => 'multipart/form_data']
]); ?>
<?= $form->field($model, 'who_paid_id')->dropDownList($users)->label('Who paid') ?
softark
(Softark)
February 26, 2018, 6:55pm
12
With this code i get an error: explode() expects parameter 2 to be string, array given.
…
error line -> $whoHasToPay = explode(",", $model->who_has_to_pay_for_it_ids);
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.