Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity::toArray() only converts Entity objects to array, not other objects #7643

Closed
waltervos opened this issue Nov 4, 2015 · 10 comments
Closed
Labels
Milestone

Comments

@waltervos
Copy link

I ran into this issue as I was trying to convert a query result to a collection (new Collection($result->toArray())) and then trying to Collection::extract(). When I follow that with a Collection::toList() or Collection::toArray() call, I get the following error:

Cannot use object of type Cake\I18n\Time as array in (..)/vendor/cakephp/cakephp/src/Collection/ExtractTrait.php on line 84

As far as I can see, this is because of Cake\I18n\Time objects present in the collection. These don't get converted to an array in Entity::toArray().

I hope this is just my failure to grasp the workings of collections fully ;), but otherwise I think we should make sure that other objects get converted to array values as well.

@waltervos waltervos changed the title Entity::toArray() does not convert objects to array Entity::toArray() only converts Entity objects to array, not other objects Nov 4, 2015
@lorenzo
Copy link
Member

lorenzo commented Nov 4, 2015

Yes, this is is the expected behavior, specially since there is a vast amount of objects that cannot be converted to arrays in a sane way. What where you trying to do?

@lorenzo lorenzo added the support label Nov 4, 2015
@lorenzo lorenzo added this to the 3.1.3 milestone Nov 4, 2015
@waltervos
Copy link
Author

I was trying to use extract from a result set. From some associated tables, actually. Something like:

$result = $this->Model->find()->where()->contain(['OtherModel.OtherOtherModel'])->first();
$collection = new Collection($result->toArray());
$extracted = $collection->extract('other_model.{*}.other_other_model.{*}.field');

There are timestamp fields in all of these tables, and those seem to be causing the problem.

Update: I wasn't sure if I could use the {_} twice, so I tried to extract from other_model.{_}.field, which gave the same error.

Edit: I just saw your reply, sure I'll get back to you,

@lorenzo
Copy link
Member

lorenzo commented Nov 4, 2015

Could you use a real example and the data you used? I'm failing to see the problem here.

@waltervos
Copy link
Author

This is my actual code:

$user = $this->Users->find()->where()->contain(['PinterestAccounts.PinterestBoards'])->first();
$user_collection = new Collection($user->toArray());
$hashtags = $user_collection->extract('pinterest_accounts.{*}.pinterest_boards.{*}.hashtag');
debug($hashtags->toList());

Here is the SQL: [link removed]

@mylux
Copy link
Contributor

mylux commented Nov 4, 2015

I have not tried to reproduce this issue, but thinking in a situation in which:

$hashtags = $user_collection->extract('pinterest_accounts.{_}.pinterest_boards.{_}.hashtag');

Will return a Collection of datetime objects, then toList method must not convert Time objects into array, but generate an array of Time objects, no?

@waltervos
Copy link
Author

@mylux I'm not entirely sure what Time objects have to do with it, since there is no time object in the hashtag field. But even when I tried something simpler, like extract('username'), I get the same error.

@lorenzo
Copy link
Member

lorenzo commented Nov 4, 2015

@waltervos I think the problem is that you are passing the collection an array that is actually a dictionary, not an iterable array. Can you try this?

$user_collection = new Collection([$user->toArray()]);

@mylux
Copy link
Contributor

mylux commented Nov 4, 2015

Good point, @lorenzo !

@waltervos
Copy link
Author

Huh, that does it, thanks!

Edit: Really, thanks a lot 💯

@lorenzo
Copy link
Member

lorenzo commented Nov 4, 2015

@waltervos alternatively you can do this:

$hashtags = collection($user-> pinterest_accounts)->extract('pinterest_boards.{*}.hashtag');

@lorenzo lorenzo closed this as completed Nov 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants