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

Cannot use object of type ether\\simplemap\\models\\PartsLegacy as array #317

Open
CreateSean opened this issue Jul 23, 2021 · 5 comments
Open

Comments

@CreateSean
Copy link

CreateSean commented Jul 23, 2021

Description

I recently upgraded an inherited site to craft 3.6.17 and maps 3.9.1 and am now getting this error with the element-api.php and I'm not sure how to resolve this.

output json from element api is

{
    "error": {
        "code": 0,
        "message": "Cannot use object of type ether\\simplemap\\models\\PartsLegacy as array"
    }
}

and full element-api.php file

<?php
use craft\elements\Entry;
use craft\helpers\UrlHelper;
// use aelvan\imager\variables\ImagerVariable;
use spacecatninja\imagerx\variables\ImagerVariable;

return [
    'endpoints' => [
        'api/places/<entryId:\d+>.json' => function($entryId) {
            return [
                'elementType' => Entry::class,
                'criteria' => ['id' => $entryId],
                'pretty' => true,
                'cache' => true,
                'one' => true,
                'transformer' => function(Entry $entry) {
                    $imager = new ImagerVariable();
                    $photoUrl = '';
                    foreach ($entry->thumbnailImage->all() as $photo) {
                        $photoOptions = array(
                            'width' => 900,
                            'height' => 600,
                            'mode' => 'fit',
                            'position' =>  $photo->getFocalPoint()
                        );
                        $transformedImage = $imager->transformImage($photo, $photoOptions);
                        $photoUrl = UrlHelper::baseUrl() . $transformedImage->getUrl();
                    }
                    $architect = array();
                    foreach ( $entry->people->all() as $person ) {
                        foreach ( $person->role->all() as $role ) {
                            if ( $role->slug == 'architect' ) {
                                $architect[] = array(
                                    'name' => $person->title,
                                    'url' => $person->getUrl()
                                );
                            }
                        }
                    }
                    $price = is_numeric($entry->price) ? '$' . number_format($entry->price) : $entry->price;
                    $addressParts = $entry->placeAddress->parts;
                    $address = $addressParts['street_number'] . ' ' . $addressParts['route_short'] . '<br />' . $addressParts['locality_short'] . ', ' . $addressParts['administrative_area_level_1_short'] . ', ' . $addressParts['postal_code_short'];
                    return [
                        'id' => $entry->id,
                        'title' => $entry->title,
                        'body' => $entry->body,
                        'forSale' => $entry->type->handle == 'propertyForSale',
                        'thumbnail' => $photoUrl,
                        'architect' => $architect,
                        'price' => $price,
                        'year' => $entry->yearBuilt,
                        'bedrooms' => $entry->bedrooms,
                        'bathrooms'=> $entry->bathrooms,
                        'listingAgent' => $entry->listingAgent,
                        'address' => $address,
                    ];
                }
            ];
        }
    ]
];

If I remove anything with $address things work as expected. How can I resolve this?

Steps to reproduce

Additional info

  • Craft version: 3.6.17
  • Maps version: 3.9.1
  • PHP version: 7.4
  • Database driver & version: mysql 5.7
  • Other Plugins: element API
@CreateSean
Copy link
Author

Anyone? 6 days and no response?

@jamesedmonston
Copy link
Member

@CreateSean does accessing the $addressParts properties with -> work? I.e. $addressParts->street_number.

@CreateSean
Copy link
Author

@jamesedmonston I don't know php and not sure where to update that line.

I tried replacing $addressParts = $entry->placeAddress->parts; with $addressParts -> $entry->placeAddress->parts; but that did not work.

please advise

@jamesedmonston
Copy link
Member

@CreateSean Try changing the following line

$address = $addressParts['street_number'] . ' ' . $addressParts['route_short'] . '<br />' . $addressParts['locality_short'] . ', ' . $addressParts['administrative_area_level_1_short'] . ', ' . $addressParts['postal_code_short'];

To

$address = $addressParts->street_number . ' ' . $addressParts->route_short . '<br />' . $addressParts->locality_short . ', ' . $addressParts->administrative_area_level_1_short . ', ' . $addressParts->postal_code_short;

@CreateSean
Copy link
Author

@jamesedmonston that did not work . I did however comment out that section and the error disappears

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants