Why does update_post_meta change the value of my meta key? - wordpress

When I use the update_post_meta() function to change a value of a key:
when it's a normal string it works
but when the string is the same as a JSON file it appends some extra string.
For example, when I save this string:
a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}
It is saved on the wp_postmeta table as:
s:162;"a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}"
When I use a short string there are no problems. How can solve this?
My code:
$edd_files='a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}';
update_post_meta($download_id,'edd_download_files',$edd_files);

The reason why this is happening is because update_post_meta() serializes the value you pass as third parameter (see update_metadata()).
Your $edd_files variable is a serialized array -not a "JSON file"- and update_post_meta() is serializing it again before saving it to the database, hence the reason why your serialized string changed like that.
I don't know why you're assigning $edd_files a serialized string but you can convert it back to an array using the maybe_unserialize() function before saving it as a post meta and then the value will be saved on the database as a serialized string as expected:
$edd_files = 'a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}';
// Convert serialized string back into an array
$edd_files = maybe_unserialize($edd_files);
update_post_meta($download_id, 'edd_download_files', $edd_files);
Result:

Related

Optional Query Params and composed data

I'm trying to figure it out how to build a query string with some important information to be fetched. For example, I have this struct I'm going to decode the query into:
SomeObject{
Names []string
Attr Attribute
}
Atribute{
Group string
City string
}
So, initially I understand passing the names in the query string should look like this:
HTTP://localhost:8000/api/retrieve?names=oneName&names=anotherName
And I can get the names[oneName anotherName] correctly. But now I must build the query to get the attributes in order to get: attributes{group{someGroup} city{Delhi}}. How should the query string be written????
Question it's not related to the HTTP request handlers, but how the params should look to get correctly
Thanks in advance

Can Dynamodb save method return haskey?

Currently I have a dynamodb table with hash key marked as DynamoDBAutoGeneratedKey. That means whenever I will save in dynamodb table hashkey will be generated automatically as UUID. I dont need to specify one by myself.
Now I would like to know if there is a way that I can have this hashkey once record is saved in the table. Is there a way that save method returns the record object from which I can get this hashkey?
Thank you,
Prasad
You can get the auto generated id from the model object after the save() method is executed successfully. Please note that DynamoDBMapper class save() method is defined as void. However, it does populate the auto generated id in the model object.
Order id is defined as auto generated key:-
#DynamoDBHashKey(attributeName = "orderId")
#DynamoDBAutoGeneratedKey
public String getOrderId() {
return orderId;
}
Order id is available in the "order" object as soon as the save() method is executed successfully:-
dynamoDBMapper.save(order);
System.out.println("Order id : " + order.getOrderId());
Output:-
Order id : f8b63e5b-eeff-43aa-bcaf-fdc245f43a7c

Set default display value for Symfony2 form field

I've got a form with some display only fields in it. These fields are usually DateTime values... but when empty/null I would like to display the string "never".
EDIT:
To be more explicit: The field should show the DateTime value from the database and if null the string 'never' should be displayed.
How should I do that?
Thanks in advance
You can use Symfony2 Data Transformers :
In the transform() function you can check if your date is null and then return the 'never' string. Otherwise return a string representation of your date.
In the reverseTransform() function you can check if the string is 'never' and then construct a null DateTime object. Otherwise, you transform the given string into a valid DateTime object with something like 'strtotime()` PHP function.

ConvertEmptyStringToNull=”false” and yet the conversion still happens

DetailsView is bound to ObjectDataSource. Inside Detailsview’s EditItemTemplate are two TextBoxes ( T1 and T2 ). T1 is mapped to update parameter of type String, while T2 is mapped to update parameter of type DateTime.
Assuming both TextBoxes contain an empty string, then when I try to update the data source by clicking on DetailsView’s Update button, ODS ( or is it perhaps DetailsView ) automatically converts T1’s empty string to null, while T2’s empty string doesn’t get converted to null. I’ve tried to prevent ODS from converting T1’s empty string to null by setting T1’s update parameter’s ConvertEmptyStringToNull property to false ( I ‘ve also set <asp:TemplateField ConvertEmptyStringToNull=”false” …>, but to no effect.
a)Any idea why T1’s empty string gets converted, while T2’s doesn’t?
b) Also, how can I prevent the conversion( BTW - I realize I could convert null back to empty string inside update method )?
thanx
a)Any idea why T1’s empty string gets
converted, while T2’s doesn’t?
T2 is a DateTime which is a value type. Value types can't be null. Well unless you use the Nullable type
b) Also, how can I prevent the
conversion( BTW - I realize I could
convert null back to empty string
inside update method )?
EDIT: I've tried to duplicate the problem above, but I could only duplicate the problem when I didn't specify ConvertEmptyStringToNull="false" in the <asp:TemplateField> of the bound control AND the <asp:Parameter> of the <asp:ObjectDataSource>. If you leave either out then you will get the null value on an empty field. With the ConvertEmptyStringToNull="false" defined in both places it does not convert the empty string to a null value. The empty string is passed correctly. You said that you did try it in both places, so I'm not sure why it's not working for you. Maybe you could show us your datasource and detailsview markup.
With that said I think it is still a good idea to make the check described below in your business class. Like you said you can convert null back to an empty string. This is how I have done it:
I have a helper class, lets call it BizObject, that contains this method:
protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}
Then in my business class's Insert/Update method I call ConvertNullToEmptyString on each string parameter:
public static bool UpdateSource(string sourceName, DateTime sourceDate)
{
sourceName = BizObject.ConvertNullToEmptyString(sourceName);
...
bool ret = UpdateSource(record);
return ret;
}

stored procedure expects parameter which was not supplied

I'm calling a stored procedure from C# .net and one of the string parameters is null. When that is the case, I pass DBNull.Value. However, I get the above error. Any ideas?
If the string is null, you will see this error. To avoid it, you can set a default parameter value in your stored proc, or you can pass DBNull.Value if your string is null.
You get this if the value of a parameter is "null" (as opposed to DBNull.Value).
Are you sure the parameter value is DBNull.Value?
Do you have access to the Stored Procedure? If so, (And if the Stored procedure logic will allow it), modify the declaration of the input parameter to add " = Null" at the end, as in
Create procedure ProcName
#MyParameterName Integer = Null,
-- con't
As...
Can you give more details like how you are calling the sproc, the parameter itself and the value. You know in your sproc you can set default values for variables.
Something to the effect of:
ALTER SprocMySproc
#myvar varchar(50)=NULL
SELECT blah FROM MyTable WHERE MyField=#myvar OR #myvar IS NULL
Your actual C# or vb.net code can then ignore sending the parameter if it is null or empty
if(!(String.IsNullOrEmpty(myVar)))
{
//pass the parameter
mySQLCommandObject.Parameters.Add("#myvar", sqldbtype.varchar).Value = myVar;
//other code...
}

Resources