I just added PHP ActiveRecord to my CodeIgniter installation. I'm running a few tests now and am very confused by a few things. I have a products table in mysql, and when I use ActiveRecord to retrieve the first row in the table, this is the output I get:
Product Object
(
[errors] =>
[attributes:ActiveRecord\Model:private] => Array
(
[id] => 1
[ext_id] => 347
[ext_type_id] => 1
[sku] => SYB-RIU-SUP
[url] => http://domain.com
[name] => SUP Wall Storage Rack
[thumbnail_url] => https://thumburl.com
[sort_order] => 20
[profitability] => 40
[last_google_update] => ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
*RECURSION*
[attribute_name:ActiveRecord\DateTime:private] => last_google_update
[date] => 2014-05-21 05:01:12
[timezone_type] => 3
[timezone] => UTC
)
[last_internal_update] =>
[discontinued] => 0
[stock_notes] =>
[deleted] => 0
[created_on] => ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
*RECURSION*
[attribute_name:ActiveRecord\DateTime:private] => created_on
[date] => 2014-03-25 22:31:58
[timezone_type] => 3
[timezone] => UTC
)
)
[__dirty:ActiveRecord\Model:private] => Array
(
)
[__readonly:ActiveRecord\Model:private] =>
[__relationships:ActiveRecord\Model:private] => Array
(
)
[__new_record:ActiveRecord\Model:private] =>
)
What do the *RECURSION* lines mean in the object? Is that expected or is that a problem? Also, let's say I need to access the last_google_update field, I do the following:
$product = Product::first();
$date = $product->last_google_update;
print_r($date);
This outputs the following:
ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
(
[errors] =>
[attributes:ActiveRecord\Model:private] => Array
(
[id] => 1
[ext_id] => 347
[ext_type_id] => 1
[sku] => SYB-RIU-SUP
[url] => http://www.storeyourboard.com/sup-storage-rack.html
[name] => SUP Wall Storage Rack
[thumbnail_url] => https://cdn2.bigcommerce.com/server2100/6p8lmnk/products/347/images/2292/standup_paddle_board_rack__43491.1392317669.225.360.jpg?c=2
[sort_order] => 20
[profitability] => 40
[last_google_update] => ActiveRecord\DateTime Object
*RECURSION*
[last_internal_update] =>
[discontinued] => 0
[stock_notes] =>
[deleted] => 0
[created_on] => ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
*RECURSION*
[attribute_name:ActiveRecord\DateTime:private] => created_on
[date] => 2014-03-25 22:31:58
[timezone_type] => 3
[timezone] => UTC
)
)
[__dirty:ActiveRecord\Model:private] => Array
(
)
[__readonly:ActiveRecord\Model:private] =>
[__relationships:ActiveRecord\Model:private] => Array
(
)
[__new_record:ActiveRecord\Model:private] =>
)
[attribute_name:ActiveRecord\DateTime:private] => last_google_update
[date] => 2014-05-21 05:01:12
[timezone_type] => 3
[timezone] => UTC
)
So the get the actual date, I tried this:
print_r($date->date);
However, this gives the following error:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined property: ActiveRecord\DateTime::$date</p>
<p>Filename: controllers/test.php</p>
<p>Line Number: 16</p>
</div>
But, if I include a call to get_active_vars() first, it works:
print_r(get_object_vars($date))
print_r($date->date);
Outputs:
Array
(
[date] => 2014-05-21 05:01:12
[timezone_type] => 3
[timezone] => UTC
)
2014-05-21 05:01:12
To summarize:
Are the *RECURSION* flags expected?
How do I work with datetime fields and how do I access the actual dates?
RECURSION is not good defenetly
I am not sure but
[model:ActiveRecord\DateTime:private] => Product Object
somehow you have product object in a dateTime so and class trying to parse it that is why recursion.
try to see configuration of the module
So solution is to use format function on the date object property.
$date->date->format('Y-m-d H:i:s')
Related
I am working on something in Wordpress and need to parse an array of objects and get some specific property information values.
I believe I am having issues be cause the property name has colons in it:
EX: id:DLM_Download_Version:private
The arrays name is $versions. This is the value of the array when i use print_r:
(
[0] => DLM_Download_Version Object
(
[id:DLM_Download_Version:private] => 208
[author:DLM_Download_Version:private] => 1
[download_id:DLM_Download_Version:private] => 196
[menu_order:DLM_Download_Version:private] => 0
[latest:DLM_Download_Version:private] =>
[date:DLM_Download_Version:private] => DateTime Object
(
[date] => 2022-03-22 12:48:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[version:DLM_Download_Version:private] => 3
[download_count:DLM_Download_Version:private] => 0
[filesize:DLM_Download_Version:private] => 2966739
[md5:DLM_Download_Version:private] =>
[sha1:DLM_Download_Version:private] =>
[sha256:DLM_Download_Version:private] =>
[crc32b:DLM_Download_Version:private] =>
[mirrors:DLM_Download_Version:private] => Array
(
[0] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-SENTIMENT-REPORT-3-21-22.pdf
)
[url:DLM_Download_Version:private] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-SENTIMENT-REPORT-3-21-22.pdf
[filename:DLM_Download_Version:private] => STS-SENTIMENT-REPORT-3-21-22.pdf
[filetype:DLM_Download_Version:private] => pdf
)
[1] => DLM_Download_Version Object
(
[id:DLM_Download_Version:private] => 200
[author:DLM_Download_Version:private] => 1
[download_id:DLM_Download_Version:private] => 196
[menu_order:DLM_Download_Version:private] => 1
[latest:DLM_Download_Version:private] =>
[date:DLM_Download_Version:private] => DateTime Object
(
[date] => 2022-03-22 12:37:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[version:DLM_Download_Version:private] => 2
[download_count:DLM_Download_Version:private] => 2
[filesize:DLM_Download_Version:private] => 3059630
[md5:DLM_Download_Version:private] =>
[sha1:DLM_Download_Version:private] =>
[sha256:DLM_Download_Version:private] =>
[crc32b:DLM_Download_Version:private] =>
[mirrors:DLM_Download_Version:private] => Array
(
[0] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/sts-weekly-sentiment-report-3-14-22.pdf
)
[url:DLM_Download_Version:private] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/sts-weekly-sentiment-report-3-14-22.pdf
[filename:DLM_Download_Version:private] => sts-weekly-sentiment-report-3-14-22.pdf
[filetype:DLM_Download_Version:private] => pdf
)
[2] => DLM_Download_Version Object
(
[id:DLM_Download_Version:private] => 197
[author:DLM_Download_Version:private] => 1
[download_id:DLM_Download_Version:private] => 196
[menu_order:DLM_Download_Version:private] => 2
[latest:DLM_Download_Version:private] =>
[date:DLM_Download_Version:private] => DateTime Object
(
[date] => 2022-03-22 12:35:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[version:DLM_Download_Version:private] => 1
[download_count:DLM_Download_Version:private] => 0
[filesize:DLM_Download_Version:private] => 2648776
[md5:DLM_Download_Version:private] =>
[sha1:DLM_Download_Version:private] =>
[sha256:DLM_Download_Version:private] =>
[crc32b:DLM_Download_Version:private] =>
[mirrors:DLM_Download_Version:private] => Array
(
[0] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-WEEKLY-SENTIMENT-REPORT-3-8-22.pdf
)
[url:DLM_Download_Version:private] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-WEEKLY-SENTIMENT-REPORT-3-8-22.pdf
[filename:DLM_Download_Version:private] => STS-WEEKLY-SENTIMENT-REPORT-3-8-22.pdf
[filetype:DLM_Download_Version:private] => pdf
)
)
How can I get just the values of id:DLM_Download_Version:private?
I have tried a foreach loop:
foreach ($versions as $version){
echo $version->{'id:DLM_Download_Version:private'}
}
which just outputs nothing.
Hi Guys :) I have something like that:
Array
(
[0] => Ideneal\OpenLoad\Entity\RemoteUploadStatus Object
(
[remoteUpload:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => Ideneal\OpenLoad\Entity\RemoteUpload Object
(
[id:Ideneal\OpenLoad\Entity\RemoteUpload:private] => xxxx
[folderId:Ideneal\OpenLoad\Entity\RemoteUpload:private] => xxxx
)
[remoteUrl:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => https://openload.co/embed/xxxxx
[status:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => finished
[bytesLoaded:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] =>
[bytesTotal:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] =>
[addedDate:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => DateTime Object
(
[date] => 2017-07-21 10:53:55.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[lastUpdateDate:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => DateTime Object
(
[date] => 2017-07-21 10:53:55.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[fileId:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => xxxx
[url:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => https://openload.co/f/xxxx <------------ Here is what I want to write to a variable
)
)
(At the end of the code is a comment)
How to do that ?
I tried something like that: print_r($myarray[0][url:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private])
and nothing.
Try to use RemoteUploadStatus object methods:
$myarray[0]->getUrl();
Ref.: https://github.com/Ideneal/OpenLoad/blob/master/src/Entity/RemoteUploadStatus.php
i am trying to get the merchantAccountId value from given below array
Array
(
[status] => 1
[result] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => Braintree_Result_Successful
[success] => 1
[_returnObjectName:Braintree_Result_Successful:private] => transaction
[transaction] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => Braintree_Transaction
[_attributes] => Array
(
[id] => 6vk28p
[status] => submitted_for_settlement
[type] => sale
[currencyIsoCode] => USD
[amount] => 800.00
[merchantAccountId] => contentorganisation
[orderId] =>
[createdAt] => DateTime Object
(
[date] => 2015-07-24 11:51:42
[timezone_type] => 3
[timezone] => UTC
)
)
)
)
)
my code is $result['result']['transaction'] .
when i print this i got this error
Fatal error: Cannot use object of type __PHP_Incomplete_Class as array in.
The result is OBJECT not array, so you have to call it by:
Array['result']->transaction->_attributes['id']
I work at Braintree. You should be able to access the merchant account ID by calling $result->transaction->merchantAccountId, (see Braintree docs).
PHP - Access MSSQL datetime column from the returned array
Array
(
[0] => Array
(
[CallId] => 45
[CallLoginId] => 1
[CustomerId] => 140
[CallOptionId] => 2
[CallTypeId] => 2
[CallStatusId] => 1
[CallDateTime] => DateTime Object
(
[date] => 2012-06-28 00:00:00
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[ContactNo] => 45151551115
[ContactPerson] => Contact Person name
[ProductId] => 1
[ProdCompanyId] => 1
[ProdCategoryId] => 1
[ModelNo] => 451212151
[ProdUnderId] => 1
[Problem] => Simple Problem Details
[Remarks] => Remarks
[Accessories] => Accessories
[CallCaseId] =>
[CallCaseDate] =>
[ServiceCharge] => 0
[CourierName] =>
[DocketNo] =>
[CompanyId] => 126
[ASPId] => 130
[InsBy] => 134
[InsDate] => DateTime Object
(
[date] => 2012-06-23 17:04:51
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[UpdBy] => 11
[UpdDate] => DateTime Object
(
[date] => 2012-06-28 18:29:23
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[FName] => Kumar
[MName] => a
[LName] => Customer
[ProductName] => LenovoDesktop420
[CallOption] => InHouse
[CallType] => H.W.Installation
[ProdCompany] => Lenovo
[ProdCategory] => Desktop
[ProdUnder] => AMC
[CallStatus] => Open
[EntityId] => 134
[InsertBy] => Bhavin Rana
)
)
how can access date time value form this returned array ?
thanks in advance.
You have an outer array with numerically indexed one element [0], which is an associative array. CallDateTime as a key of that array is a DateTime object.
$array[0]['CallDateTime']->format('Y-m-d H:i:s') // 2012-06-28 00:00:00
$array[0]['CallDateTime']->getTimezone()->getName() // Asia/Kolkata
// Same with InsDate and UpdDate
$array[0]['InsDate']->format('Y-m-d H:i:s') // 2012-06-23 17:04:51
$array[0]['InsDate']->getTimezone()->getName() // Asia/Kolkata
For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.
Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?
I also tried this with no luck.
$object = $buy[1];
$title = $object->title[0];
echo "$title";
Thanks
This is what the api gives me.
stdClass Object
(
[id] => 1
[name] => toronto
[date_modified] => 2011-03-08 13:07:10
[tax_rate_provincial] =>
)
<br/>
Array
(
[0] => stdClass Object
(
[0] => stdClass Object
(
[id] => 28131844
[full_date] => 20110506
[end_date] => 20110511
[city] => toronto
[saved] => 1651
[discount_percentage] => 52
[deal_option] => Array
(
[0] => stdClass Object
(
[id] => 2600
[title] =>
[date_modified] => 0000-00-00 00:00:00
[date_created] => 2011-05-03 14:33:58
[value] => 3150
[price] => 1499
[deal_id] => 28131844
[is_default] => 0
)
)
[options] =>
[option_quantity] =>
[option_remaining] =>
[purchase_limit] => 1
[gift_limit] => 0
There is a special evil syntax to bypass numeric object attributes:
print $obj->{'0'}->{'0'}->city;
Is the correct syntax, and equivalent to the path you already determined.
Your second example is an array however, so it's probably:
print $array[0]->{'0'}->city;
The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.