Pimcore: Setting DateTime Class Fields on Objects - datetime

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.

Related

Why wordpress get_post_meta not returning anything, when my DB has value?

database data
return array( get_post_meta(12785, 'attribute_settings', true));
or
return array( get_post_meta("12785", 'attribute_settings', true));
it returns [""]
changing the key with another value, it returns the value in the database.
You mention that if you change the key, you get the value you want. Looking at your screenshot, you've got duplicate keys for attribute_settings. How you got a duplicate key in there, I'm not sure (did you add them directly in phpMyAdmin? Using update_post_meta() will update the record if it exists)
Now, I haven't seen the case before where a duplicate post_id:meta_key pair exists, but I'd wager it's returning at the NULL value since it's first. Delete that entry and you should be good.
Documentation & Function Reference
Function
Linked Description
update_post_meta()
Updates a post meta field based on the given post ID.
Try this below code.
$attribute_settings = array();
$attribute_settings[] = get_post_meta( 12785, 'attribute_settings', true );
return $attribute_settings;

SilverStripe edit gridfield success message on save

What's the easiest way to edit the default success message when saving an item in GridField edit view?
The message seems to be in a variable in class GridFieldDetailForm within method doSave.
$message = _t(
'GridFieldDetailForm.Saved',
'Saved {name} {link}',
array(
'name' => $this->record->i18n_singular_name(),
'link' => $link
)
);
Since the message uses the _t() function it will attempt to fetch the value defined in the lang file corresponding to the current user's locale. The default string defined in the function is just a fallback for when no translation could be found within the lang files.
To change the message you can update your site's yml lang file located in mysite/lang/{LANGUAGE_CODE}.yml
For english this would be:
# mysite/lang/en.yml
# remember to flush after editing me :-)
en:
GridFieldDetailForm:
Saved: 'My custom message using {name} and here is a link to the object: {link}'
https://docs.silverstripe.org/en/3.4/developer_guides/i18n/
Something like this should work for specific implementations
$form = $gridField->getConfig()->getComponentByType('GridFieldDetailForm');
$form->setItemEditFormCallback(function($form, $itemRequest)
{
// Replace save action with custom method in here
});
For more general implementations, you'll likely want to extend GridFieldDetailForm and override doSave, then replace the GridFieldDetailForm component with your custom class.

Symfony 2 custom DQL request inside an array field

I'm trying to make a custom request in my repository with a WHERE clause inside an array field. I tried something like that, not working, but can better show my problem :
$qb ->andWhere( "p.addresses[:index] = :address" )
->setParameter( "index" , $p_idLang )
->setParameter( "address" , $p_address );
Extracted from the documentation about array type:
Maps and converts array data based on PHP serialization. If you need
to store an exact representation of your array data, you should
consider using this type as it uses serialization to represent an
exact copy of your array as string in the database. Values retrieved
from the database are always converted to PHP’s array type using
deserialization or null if no data is present.
Your query doesn't make sense. You have a few options though:
Retrieve p.adresses and check using php if p.adresses[$index] = $address
Try something much less reliable but that could work:
$val_length = strlen($p_address);
$qb ->andWhere( "p.addresses LIKE :indexAddress" )
->setParameter( "indexAddress" , "%i:$p_idLang;s:$val_length:$p_address%" );
Create a new entity and a relation oneToMany between this entity and the new one.
I'd definetely try option 3. Option 1 isn't an option if the array is big or will become big in the future. I wouldn't go for option 2, but as an experiment could be worth trying.

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

Passing arguments to a Drupal6 View

I'm building an Address Book view based on a simple CCK that I built.
I need to pass the initial letter as argument to the view in order to show only the elements starting with the received letter. How can I do it using Drupal Views? I've searched in arguments but cannot find a way to get substring support but only node/user references or CCK fields.
If you use this:
$view = views_get_view('masvisitados');
$view->execute();
print_r( $view->render() );
It will render with the markup, but If you just want the data (like me), you can try this:
$view = views_get_view('masvisitados');
$view->preview('Mas visitados', 4);
$view = $view->result;
print_r( $view );
It returns an array.
That's exactly what a "glossary view" does. Navigate to /admin/build/views on your site. If the default view called "glossary" is disabled, enable it. You can now edit/inspect/try it to see how it works. The key is in the 'Node: Title' argument, where the "glossary mode" checkbox is selected. For your address book, you can duplicate it and add a filter on the desired node type.
...
$view->set_exposed_input( array('field1' => data1, 'field2' => 'some data') );
...

Resources