laravel: Undefined property: stdClass::$game - php

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.

Related

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

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?

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

Xpath stuck in while loop?

I need to check xml feeder updated date and updated date from my database and get just updated date that are not exists in my database.
I am doing that because i need to get just odds changes from last time that parser start.
But there is problem it's stuck and nothing happened, just loading page, all day.
Here is code:
$updDate = $this->PDO->prepare("select distinct FixtureMatch_Id,UpdatedDate from odds");
$updDate->execute();
$updDate->setFetchMode(PDO::FETCH_ASSOC);
while($row = $updDate->fetch()){
$odds=$this->soccer->GetAllOddsByFixtureMatchId(
array("fixtureMatch_Id"=>$row['FixtureMatch_Id']))
->xpath("//Odds[not(contains(UpdatedDate,$row['UpdatedDate']))]");
echo $row['FixtureMatch_Id'] . ' : ' . $row['UpdatedDate'] . '<br>';
}
}
this is how it looks when i var dump without xpath like this:
$odds=$this->soccer->GetAllOddsByFixtureMatchId(
array("fixtureMatch_Id"=>$row['FixtureMatch_Id']))->OddsList->Odds;
xml
object(SimpleXMLElement)[11]
public 'FixtureMatch_Id' => string '325006' (length=6)
public 'Bookmaker' => string 'Pinnacle' (length=8)
public 'UpdatedDate' => string '2015-05-23T14:58:51.213' (length=23)
public 'Type' => string 'Over/Under 2.5' (length=14)
public 'HomeOdds' => string '1.85' (length=4)
public 'AwayOdds' => string '2.08' (length=4)
Update: In database is abount 16000 rows in odds table, is it problem?

Laravel: retrieving data from a select statement

I Tried retrieving my data from $result via:
$result->pluck('request_time');
and
$result->request_time;
Both give me a non-object error,
var_dump returns:
array (size=1)
0 =>
object(stdClass)[167]
public 'id' => string 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' (length=38)
public 'request_time' => string '2015-05-10 17:01:02' (length=19)
public 'account_id' => int XXXXXXX
public 'NOW()' => string '2015-05-14 02:03:12' (length=19)
How do I retrieve the data without using the DB::table->pluck method?
It's giving you a non-object error because $result is an array.
Try $result[0]->request_time;
Try
$result = DB::table('yourtable')->where('foo', 'bar')->first();
dd($result->request_time);

Looping through objects

Suppose you have this array with objects:
array
0 =>
object(User\Entity\User)[297]
public 'id' => int 1
private 'first_name' => string 'Peter' (length=5)
private 'last_name' => string 'Johnson' (length=7)
private 'initials' => string 'J.J.' (length=4)
private 'email' => string 'test#gmail.com' (length=21)
1 =>
object(User\Entity\User)[296]
public 'id' => int 2
private 'first_name' => string 'Edith' (length=8)
private 'last_name' => string 'Peters' (length=7)
private 'initials' => string 'R.J.' (length=4)
private 'email' => string 'edit#gmail.com' (length=26)
Now i want to put them in a table. But since i want to make it universal i try to do it on an abstract way.
I have a $aColnames array in the function below in to be able to only show the get the values of the fields i want to see in the table.
This is the method i am trying to build:
private function generateTable()
{
foreach($this->aData as $aData){
$this->sTable.= '<tr>';
foreach($this->aColnames as $sColname){
$this->sTable.= '<td>';
/****What code goes here ****/
$this->sTable.= '</td>';
}
$this->sTable.= '</tr>';
}
}
The question is no how do i get the values from the objects? Do i need to instantiate the objects each time? Can anyone help me out please?
No, you do not need to instantiate the objects again. They are instantiated when you placed them in the array in the first place!
You will however need to either declare these variables public, so that they may be accessed, or have some sort of getter functions.

Categories