Silverstripe could not update record - silverstripe

I have following code in Silverstripe
<?php
$product= Product::get()->filter("ProductCode", $UniqueCode)->First();
if($product){
$product->Stock = "250";
$product->Name = "Abcd 123";
$product->write();
echo "New Stock = ".$product->Stock; //this prints the OLD value not the NEW one. Nor database is updated.
}
?>
Update:
If I do $product->Name = "Abcd 123";, the Name' field is getting updated, but not theStock`
This didn't work. The Stock field of Product table is not updated. Can anybody tell me where I went wrong?

I have fixed the problem. For someone who step up this thread with same issue in future.
Actually Stock field is integer and i was supplying string value. You need to convert string into integer before supplying to object. For example:
$updatedvalue = "250";
$updatedvalue = (int)$updatedvalue;

Related

Symfony 5 Convert string to datetime

In Symfony 5 I have a form where users enter into text fields and also a date. This is then redirected into a display controller that displays matching rows from the database. The date needed to be converted into a string for the redirect to work.
$firstName = $findPt->getFirstName();
$surname = $findPt->getSurname();
$username = $findPt->getUsername();
$dob = $findPt->getDateOfBirth();
$dobStringValue = $dob->format('Y-m-d');
return $this->redirectToRoute('app_displayClients', ['firstName' => $firstName,
'surname' => $surname,
'username' => $username,
'dob' => $dobStringValue]);
However in the display controller I then need to convert it back into a datetime to use it but that doesn't seem possible. I've tried various options, such as $dobDateTime= new DateTime($dateStr);
Please let me know if this question isn't clear or you need more information.
Many thanks in advance for any help.
You can reconvert it using DateTime::createFromFormat
$dobStringValue = $dob->format('Y-m-d');
$dobReconverted = \DateTime::createFromFormat('Y-m-d', $dobStringValue);
Thanks Agnohendrix, that was helpful. I didn't need to format as it was already a string but used $dobReconverted = \DateTime::createFromFormat('Y-m-d', $dobStringValue);
The \ seemed to have been what made it work, maybe this is something to do with using Symfony.
It works with "\Datetime" and not with "Datetime" because "Datetime" needs to be defined with a "use Datetime" to work; that's why the error "Did you forget a use statement for..." came out.

ACF User fileld for subscriber not return value

I'm using WordPress ACF plugin to store users' data.
The values I'm calling in template are as below:
echo $uid=get_current_user_id();
echo $lesson_order = get_field('lesson_order', 'user_'.$uid);
echo $last_lesson_time = get_field('last_lesson_time', "user_".$uid);
The values returned for admins are correct, but when logged in as a subscriber, the code returns empty values. I also checked user ids, which appear to be correct.
Can someone give me a solution to this. Thanks a lot.
I've tested both option on a local project and both were working but have you tried retrieving the user ID using this instead?
<?php
$current_user = wp_get_current_user();
echo "Current User ID " . $current_user->ID;
?>
If it still doesn't work, can you show a little more of your code?
You can also check that answer for more information : https://wordpress.stackexchange.com/questions/163407/get-current-user-id-returns-0

Pimcore: Setting DateTime Class Fields on Objects

I'm writing an importer and am getting stuck with actually creating the objects. Specifically, I'm having troubles with DateTime fields. I have a class called blogArticle and I'm using them just as the Pimcore demo data uses them for blog articles.
$newPost = new Object\BlogArticle();
$newPost->setCreationDate( time() );
$newPost->setPublished( true );
$newPost->setText( $text ); // text input field
$newPost->setTitle( $title ); // text input field
$newPost->setDate( $date ); // datetime input field
$newPost->setKey( \Pimcore\File::getValidFilename( $key ) );
$newPost->setParentId( $id );
$newPost->save();
The exact error I am getting is:
Whoops\Exception\ErrorException thrown with message "Call to a member function getTimestamp() on a non-object"
Stacktrace:
#0 Whoops\Exception\ErrorException in /.../pimcore/models/Object/ClassDefinition/Data/Datetime.php:73
I cannot find anything in the documentation apart from how the value is stored in the database for this field type. Literally zero documentation on how to appropriately assign values to class fields per field type.
SOLUTION
Thanks to Igor Benko on solving this one!
$newPost_date = new DateTime( "2016-07-01 12:10:37" );
$newPost->setDate( $newPost_date );
It seems that your system is still set to use the Zend_Date. DateTime is used only if you have a clean install of Pimcore 4, otherwise the compatibility layer is turned on by default after the update. The compatibility layer uses Zend_Date instead.
In your system.php you have to turn the flag useZendDate to false in order to use DateTime class.
You need to pass an instance of DateTime class to the setter. Something like this:
$date=new DateTime("2016-07-01 12:10:37");
$newPost->setDate($date);
See this:
https://www.pimcore.org/wiki/display/PIMCORE4/Update+from+Version+3.x+to+Version+4#UpdatefromVersion3.xtoVersion4-PHP%27sDateTimereplacesZend_Date
EDIT: Updated the answer after #GrafikMatthew updated his question.

Wordpress custom field in post database

I have added a field in wordpress posts table named "parent_location". How to get the value of this field in db front end? I have tried get_post_custom_values('parent_location', $post_id) function but it not working. Can any body help me out?
Thanks in advance, don't down vote
You added the field as a new column in wp_posts or added a new metadata entry to a single post? If its to a post then just do a get_post_meta(postID, 'parent_location', true); It its a new column into wp_posts, then should you not be able to just to a standard;
<?php
$my_id = 100;
$post = get_post($my_id);
$parent_loc = $post->parent_location;
?>
Not sure if that will work, I've not tried to get a custom column via that method before. If it doesn't, just roll your own SQL query; http://codex.wordpress.org/Class_Reference/wpdb

Theme CCK fieldset

I am attempting to use the CCK theme_fieldgroup_fieldset($elements) hook to convert the fieldset to a two column layout.
I thought that this shouldn't be to hard because the individual fields are in the $elements variable so all I have to do is iterate over them and print them individually. the problem is that I have no way to tell if they have been excluded from display on the "Display Fields" tab of the content type.
Does anyone have any ideas for me? Am I trying to do this the hard way or what am I missing?
Following is the solution that I came up with. The biggest problem is that it requires a database query for every field. These isn't the greatest, but it works so what can you say?
function _brioratheme_include_cck($field) {
$query = "SELECT display_settings AS ds FROM {content_node_field_instance} WHERE field_name = '%s' LIMIT 1";
$result = db_query($query, $field);
if ($result) {
$row = db_fetch_object($result);
$display_settings = unserialize($row->ds);
return !$display_settings['full']['exclude'];
}
}

Resources