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.
Related
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
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.
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?
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?
I have the need to display the bucket contents on my S3 and I am using Amazon's PHP SDK.
My code is simply
$objects = $s3->list_objects("mybucket",array("max-keys"=>5));
var_dump($objects);
The response I get from the server is very complicated for me to understand -
It's essence is
Object(CFResponse)[107]
public 'header' =>
array (size=11)
'x-amz-id-2' => string
...
public 'body' =>
object(CFSimpleXML)[106]
public '#attributes' =>
array (size=1)
'ns' => string 'http://s3.amazonaws.com/doc/2006-03-01/' (length=39)
public 'Name' => string 'cdneu.2yourfacecdn.com' (length=22)
public 'Prefix' =>
object(CFSimpleXML)[3]
public 'Marker' =>
object(CFSimpleXML)[105]
public 'MaxKeys' => string '5' (length=1)
public 'IsTruncated' => string 'true' (length=4)
public 'Contents' =>
array (size=5)
0 =>
object(CFSimpleXML)[104]
...
1 =>
object(CFSimpleXML)[103]
...
2 =>
object(CFSimpleXML)[102]
...
3 =>
object(CFSimpleXML)[101]
...
4 =>
object(CFSimpleXML)[100]
...
public 'status' => int 200
I believe the part under the 'Contents' is what I'm looking for but how do I access it ? I'm used to receiving arrays where I can figure out what the keys are and how to access but this here is difficult for me ,
Any guesses?
try this in order to list the key element of each object:
$s3 = new AmazonS3();
$objects = $s3->list_objects("YOUR BUCKET NAME",array("max-keys"=>5));
foreach ($objects->body->Contents as $item){
print_r($item->Key."");
}
You can access contents as follows
$contents = $objects['Contents'];