how to get key from object(stdClass)[key]? - php

I want to create new array base on object with structure like this :
object(stdClass)[33]
public 'IKSindividuList' =>
array (size=6)
0 =>
object(stdClass)[27] // <---- i need to get the 27
public 'indikator' => string 'nik' (length=3)
public 'value' => string '123654789' (length=9)
1 =>
object(stdClass)[28]
public 'indikator' => string 'name' (length=4)
public 'value' => string 'afdikage' (length=8)
and the new array that i want to create, more or less should look like this :
array(size=6)
27 => // and put the 27 as index number
array(size=1)
'nik' => '123654789'
28 =>
array(size=1)
'name' => 'afdikage'
I don't have any idea what is that number and how to get it. Can someone help me with this?

If you use XDebug you get this in the brackets, if you don't it will be after the hash like this object(stdClass)#1
This is object identifier, which you should not rely or care in real life. See: http://php.net/manual/en/language.oop5.references.php
You can get the object ID using spl_object_id, but that seems like a bad idea. Consider if the object gets destroyed that ID might get reused and your will not have any real meaning.
You can also take a look at a similar SO question What is # next to object(someClass) in var_dump of an object? I have an inference. Am I right?

Related

How to extends properly Cms Page and Block WebAPI

I would like to extends/override the APIs of CMS Pages and Blocks to add custom fields (for example, an array of store_ids to allow the creation of a CMS block on different stores with an webapi call, it's my main goal).
I tried many things, but this does not seem clean, standards-compliant. I have the impression of overloading the whole module CMS (data interface, repository interface, model, repository factory, etc..) just for adding new fields in webapi.
I will explain what I have done for now :
1/ I wrote an new data api \BlockInterface who extends the core interface to add my new field (store_id) and declare his getter and setter methods.
namespace Reflet\CmsExtension\Api\Data;
interface BlockInterface extends \Magento\Cms\Api\Data\BlockInterface
{
/**
* New fields
*/
const STORE_ID = 'store_id';
/**
* Get Store Ids
*
* #return array
*/
public function getStoreId();
/**
* Set Store Ids
*
* #param array $storeIds
* #return \Reflet\CmsExtension\Api\Data\BlockInterface
*/
public function setStoreId($storeIds);
}
2/ I implements the Data Interface in the new \Block model object.
3/ With many in the di.xml file, I override the block model type.
4/ I reimplement the api \BlockRepositoryInterface and the webapi.xml file to change the data type used in the repository.
5/ I testing the getById() and the save() methods for the block #1 with simple repository call and webapi.
Here, is my result :
1/ If, I get the block #1 with the repository, the store_id is load in the result object.
array (size=12)
'row_id' => string '1' (length=1)
'block_id' => string '1' (length=1)
'created_in' => string '1' (length=1)
'updated_in' => string '2147483647' (length=10)
'title' => string 'Catalog Events Lister' (length=21)
'identifier' => string 'catalog_events_lister' (length=21)
'content' => string '{{block class="Magento\\CatalogEvent\\Block\\Event\\Lister" name="catalog.event.lister" template="lister.phtml"}}' (length=113)
'creation_time' => string '2017-02-14 14:33:20' (length=19)
'update_time' => string '2017-02-14 14:33:20' (length=19)
'is_active' => string '1' (length=1)
'store_id' =>
array (size=1)
0 => string '0' (length=1)
'stores' =>
array (size=1)
0 => string '0' (length=1)
2/ If, I get the block #1 with the WebAPI, the store_id is also load in the result object.
public 'store_id' =>
array (size=1)
0 => string '0' (length=1)
public 'id' => int 1
public 'identifier' => string 'catalog_events_lister' (length=21)
public 'title' => string 'Catalog Events Lister' (length=21)
public 'content' => string '{{block class="Magento\\CatalogEvent\\Block\\Event\\Lister" name="catalog.event.lister" template="lister.phtml"}}' (length=113)
public 'creation_time' => string '2017-02-14 14:33:20' (length=19)
public 'update_time' => string '2017-02-14 14:33:20' (length=19)
public 'active' => boolean true
3/ If, I save the block #1 with the WebAPI, an error occurs, he load the wrong BlockInterface. I need to reimplement the BlockRepository ? I don't think it's the good and cleaner method (...) ?
public 'message' => string 'Property "StoreId" does not have corresponding setter in class "Magento\Cms\Api\Data\BlockInterface".' (length=101)
public 'trace' => string '#0 /var/www/vhosts/reflet.com/storemanager/vendor/magento/framework/Reflection/NameFinder.php(59): Magento\Framework\Reflection\NameFinder->findAccessorMethodName(Object(Zend\Code\Reflection\ClassReflection), 'StoreId', 'getStoreId', 'isStoreId')
#1 /var/www/vhosts/reflet.com/storemanager/vendor/magento/framework/Webapi/ServiceInputProcessor.php(158): Magento\Framework\Reflection\NameFinder->getGetterMethodName(Object(Zend\Code\Reflection\ClassReflection), 'StoreId')
#2 /var/www/vhosts/reflet.com/storemanag'... (length=2239)
For this example, I use the 'store_id' only, but in the future, it's must be works with all custom fields in Page or Block object.
How do you recommend this modification to be as clean as possible and limit conflicts in the future ? Do you have an example ?
If you want to test, you can find in this archive, my two current Magento modules (Reflet_Api an simple adapter to call REST WebApi and Reflet_CmsExtension to override CMS Blocks) : https://drive.google.com/file/d/0B12trf96j0ALSjhZdmJPTzBPT0U/view
Thank you for your answers.
Best regards.
Jonathan

laravel: Undefined property: stdClass::$game

i am trying to get some values out of an Std Object in Laravel.
I can get anything out, with no problem, but not the last one
my object looks like this (var_dump())
object(stdClass)[208]
public 'id' => int 2
public 'vorname' => string 'Thomas' (length=5)
public 'nachname' => string 'Kemmet' (length=5)
public 'geburtstag' => string '1988-05-05' (length=10)
public 'verein_id' => int 1
public 'verein_name' => string 'Kolandorf' (length=20)
public 'game' => string '2016-10-28' (length=10)
when i do
echo $spieler->id;
i get the id. But when i try
echo $spieler->game;
i get an error with Undefined property: stdClass::$game
why is it like this? and how to get the last value...
Make sure that the game field is a string in the database you retrieve it from.
Try changing the order of the fields in order to see if its always the last one which has the problem.

CakePHP- saving associated model data

I have two models .. One is called Schedule and the other ScheduleContact.
Schedule belongsTo ScheduleContact
From an action belonging to scheduleController, I want to save values coming from a serialized form for the main model as well as the associated model.
In my view I have the following
<?php echo $this->Form->input('Schedule.start_at', array('id' => 'start_at', 'type' => 'hidden')); ?>
<?php echo $this->Form->input('Schedule.finish_at', array('id' => 'finish_at', 'type' => 'hidden')); ?>
<?php echo $this->Form->input('ScheduleContact.0.name', array('id' => 'name','div' => 'inline-input')); ?>
<?php echo $this->Form->input('ScheduleContact.0.surname', array('id' => 'surname','div' => 'inline-input compact')); ?>
etc..
The request data comes in this form (from var_dump($this->request->data)):
array (size=3)
'Schedule' =>
array (size=3)
'start_at' => string '2014-3-25 11:15' (length=15)
'finish_at' => string '2014-03-25 11:30:00' (length=19)
'donation_method_id' => string '1' (length=1)
'ScheduleContact' =>
array (size=1)
0 =>
array (size=6)
'name' => string 'Jane' (length=4)
'surname' => string 'Powell' (length=6)
'address' => string 'Hamilton Hodell, 5th Floor 66-68 Margaret Street' length=39)
'city' => string 'London' (length=6)
'tel_no' => string '+44(0)20-7636 1221' (length=18)
'email' => string 'powell_jane#gmail.com' (length=22)
'log' =>
array (size=3)
'ds' => string 'default' (length=7)
'sql' => string 'SELECT `Schedule`.`id`, `Schedule`.`donation_method_id`, `Schedule`.`schedule_contact_id`, `Schedule`.`start_at`, `Schedule`.`finish_at`, `DonationMethod`.`id`, `DonationMethod`.`donation_method`, `DonationMethod`.`recovery_time`, `DonationMethod`.`duration`, `ScheduleContact`.`id`, `ScheduleContact`.`name`, `ScheduleContact`.`surname`, `ScheduleContact`.`address`, `ScheduleContact`.`city`, `ScheduleContact`.`tel_no`, `ScheduleContact`.`email`, `ScheduleContact`.`donor_id` FROM `blood_services_db`.`schedule'... (length=852)
'hash' => string '5f2b2b3a462f6555cb5290fb49c42df04a7948e0' (length=40)
Finally I am trying to save the data like so :
if($this->Schedule->saveAssociated($this->request->data)){
which is not saving anything to the database, therefore the this if condition is never met.
What could be wrong ? Thanks
Basically the problem was that saveAssociated needs to be applied in the main model. With that in mind, as it was set up above, the main model had a bolongsTo relationship, which means that the current model contains the foreign key., which is also indeed correct. So where is the problem you might ask?
The problem is that saveAsocciated is equivalent to
$user = $this->User->save($this->request->data);
// If the user was saved, Now we add this information to the data
// and save the Profile.
if (!empty($user)) {
// The ID of the newly created user has been set
// as $this->User->id.
$this->request->data['Profile']['user_id'] = $this->User->id;
}
Which means that it attempts to save the main model first. Therefore my setup was incorrect since the main model needed to be ScheduleContact, since the foreign key will be saved inside the Schedule model after data in ScheduleContact id successfully inserted.
There you go... CakePHP is Great once you get past that learning curve.
Your Model-Name is breaking Cake's naming convention. It should be:
ScheduleContact
A few questions to give you a better answer:
Can you add the Model Association Variables ($belongsTo, etc)?
Do you use Containable behaviour?

accessing google calendar data

I'm trying to get some basic event info from a public google calendar. In PHP it looks like this:
$xml = simplexml_load_file('http://www.google.com/calendar/feeds/[username]/public/basic?start-min=2014-01-01T00:00:00&start-max=2016-03-24T23:59:59');
var_dump($xml);
This brings back the XML data as expected, with the event data.
public 'id' => string 'http://www.google.com/calendar/feeds/[username]/public/basic/k524jsujs242bag0fsqfveiij8' (length=98)
public 'published' => string '2014-01-29T21:04:51.000Z' (length=24)
public 'updated' => string '2014-01-29T22:12:20.000Z' (length=24)
public 'category' =>
object(SimpleXMLElement)[1996]
public '#attributes' =>
array (size=2)
'scheme' => string 'http://schemas.google.com/g/2005#kind' (length=37)
'term' => string 'http://schemas.google.com/g/2005#event' (length=38)
public 'title' => string 'busy' (length=4)
public 'summary' => string 'When: Thu Jan 30, 2014 11am to 12pm PST<br>' (length=49)
My problem is that in the events, none of the Titles are populated-- they all simply show "busy", even though there is a valid title saved in the actual calendar event.
Anyone know how to get the actual event title?
Problem was that the calendar, while public, wasn't set in the Google Apps admin to share ALL event data.
See here for more info: https://productforums.google.com/forum/#!topic/calendar/-YcGbl1uw5Y

php mvc change default url pattern

I'm using yaf php framework
I want to get param array.
ex:
My url:... mvcSample/svc/saveUser/user1/pass/a#b.c/joe/foo
My output params dump:
array (size=3)
'user1' => string 'pass' (length=4)
'a#b.c' => string 'joe' (length=3)
'foo' => null
I want:
array (size=5)
1 =>string 'user1'
2 => string 'pass'
3 => string 'a#b.c'
4 => string 'joe'
5 =>string 'foo'
How to change default url pattern ?
Thank you
Since Yaf is very well undocumented, the only option I can think about is to parse URI by Your own:
$parts = explode('/', $this->getRequest()->getRequestUri());
But You will get garbage on begining of $parts array.
Propably, sooner or later You will start to trying custom URL routing (some samples You can find here: http://docs.php.net/manual/da/yaf-route-regex.construct.php), which will allow You to parse URLs using regexps and pass matched groups to controller:
/* in Bootstrap.php */
$dispatcher->getRouter()->addRoute('saveUser',
new Yaf_Route_Regex(
',^/mvcSample/svc/saveUser/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$,',
array(
'controller' => 'svc',
'action' => 'saveUser',
),
array(
1 => 'username',
2 => 'password',
3 => 'email',
4 => 'firstname',
5 => 'somefooshmoo',
),
)
);

Categories