Cakephp 3 “Trying to get property of non-object” error with h() when Null - cakephp-3.0

OK, I have a view template where I want to display an associated value from my 'item' entity:
<?= h($item->itemgroup->groupname)?>
If the $item->itemgroup_id is NULL, I get the error:
Trying to get property of non-object
It also errors without the h() function. However, if I change the view code to:
<?= h($item['itemgroup']['groupname']) ?>
It does not error, and displays a blank as expected.
Is it necessary to update all the baked view template code where a value is potentially NULL? Or is it a matter of database setup (i.e., not using NULL for a field that can be potentially blank)?
Thanks in advance for any insight or advice?
Cheers,
D.

Is it necessary to update all the baked view template code where a value is potentially NULL? Or is it a matter of database setup (i.e., not using NULL for a field that can be potentially blank)?
Yes, it is.
To avoid error, just use :
<?=$item->itemgroup!==null ? h($item->itemgroup->groupname) : ''?>

Related

Inserting and retrieving information from MySQL field in Laravel

I have a JSON field which I am experimenting with but I am having a bit of trouble with it.
I have added the following to my Customer model:
protected $casts = [
'billingConfig' => 'array'
];
And I updated a test field using the following in my controller:
$customer->billingConfig = ['attachableType' => $request->attachmentsConfig];
$customer->save();
After this, the following appears in my database:
{"attachableType": "combined"}
Now, when I go to grab this specific value through my blade:
{{$customer->billingConfig->attachableType}}
I get "Trying to get property 'attachableType' of non-object"
But when I use the below:
{{$customer->billingConfig['attachableType']}}
I get the "combined" value I was looking for.
I was using this guide: https://www.qcode.in/use-mysql-json-field-in-laravel/, and I guess I wanted to make sure I was doing everything right and their method was wrong or I had goofed up somewhere.
JSON data fields are being read as an array, which you can not call a property for, that's what I believe the reason for the error you got when called
{{$customer->billingConfig->attachableType}}
because it's an array, not a data object (like the case in javascript).
Thanks for sharing.

Django save/update not change data in database

Django 1.11.7
MySQL
I was trying to change the value of an object like this:
# change the value of the filed and save
def patch(...):
instance.field_name = new_name
instance.save()
print(instance.filed_name)
When I run the code I got the print result as new_name. But when I check the database manually I got the result as old_name.
Then I tried ways like:
instance.save(update_fields=['field'])
and
ModelName.objects.filter(id=instance.id).update(field_name=new_name)
but get the above problem as well. And meanwhile, the project runs perfectly functional except for this segment of code.
Any idea what caused this problem or suggestion on how to solve it?
Is that piece of code inside a transaction? Maybe the transaction gets rolledback somewhere later.
When you read from the DB are you inside a transaction? Some transaction modes may not show you this change.
Are you sure that field_name is the correct field name? Maybe you have a typo and you just set a property of the object without changing model field. From what I see you sometimes type "field_name" and sometimes "filed_name"

Complie Error: Method or Data Member not found, but it does exist

Having a strange problem in vba access. I have a text control control called txtUserName on a form and am trying to get the contents which is all but child's play. When I try to run or complie, I get the Method or Data Member not found error. However, when I type me. Intellisense shows txtUserName. I have gone as far as copying and pasting the name from the control's properties and still get the error. What am I missing? I am completely perplexed.
Thanks in advance
Don
In this case, I was just building a SQL string. I did find the problem, there was a bad control reference, however, it was not the one that was highlighted (highlighted was the first value in an insert into string and it was actually the 3rd value)

How to fix undefined index error by possibly having more than one select name

I am having an undefined index: ISBN error when running the following code. I think it is because I don't have ISBN for the name of my select, but I only want one select. How can I solve this?
<select name="GroupID">
......
</select>
<?php
$GroupID=$_POST['GroupID'];
$ISBN=$_POST['ISBN'];
?>
The name attribute is used to determine which values are actually posted to the server, so you'll want to ensure that you have one for each value that you are expecting to send :
<select name='GroupID'>
<!-- Omitted for brevity -->
</select>
<input name='ISBN' />
This would pass the selected option for GroupID to the server and whatever value was stored in your ISBN element. If these two have some type of relationship (as you mentioned you wanted to only use a single <select> element) then you'll likely need to clarify how they are related.
Based on their relationship (if one exists), you could possibly use Javascript to set another hidden ISBN element so that it is posted separately to the server.
Use of isset(), empty() conditions before using any variable should rectify this.
This error comes up when a variable is unset.
Try this : $ISBN= filter_input($_POST['ISBN'], 'value');
Also refer : Undefined index: id in php

Can't save the data using wbragancia widget yii2 for more than one dynamic form in single view

I am new in yii2 and i am using wbraganca/yii2-dynamicform widget in my view. My question is that I have to use more than two dynamic form in a single view. For single dynamic widget it works fine while saving from controller but others are saving just the last data of dynamic widget, while inspecting the post data in network array index for others except first dynamic form are not increasing (there index is only zero). Would you please suggest how should I have to do. How can I use multiple dynamic-form widget in single view. Thank you in advance.
I believe the problem must be in your view. You should ensure that each control has a unique name and then loop through the post variables in the controller.
<?= $form->field($modelx, "[{$i}]MyVariableName", ['labelOptions' => ['label' => false]])->textInput(['name' =>'myClassNameHere'."{$block}[$i][MyVariableName]"]) ?>
See the 'name' option in the code above. It gives a unique name to each field. Here the $block variable is a unique code I append to each widgetItem class in the DynamicFormWidget.
If I now look at my POST data, I see something like the following:
myClassNameHere0[0][MyVariableName]:1
myClassNameHere1[0][MyVariableName]:11
If this does not help - post a simply example that you cannot get working and I will have a look at it.