onpage-dev / onpage-php

4 stars 5 forks source link

Issue with val() function #7

Closed saracomin96 closed 11 months ago

saracomin96 commented 11 months ago

Hello,

I'm integrating this library to my wordpress site. I have a problem: I use the val() function to retrieve the field values, for some fields it works correctly but for some it generates an error and I don't understand why

this is the error I have, I don't know if it can help to understand:

Fatal error: Uncaught TypeError: OnPage\Thing::values(): Return value must be of type Illuminate\Support\Collection, array returned in /nas/content/live/websitestg/wp-content/themes/astra-child/my-project/vendor/onpage-dev/onpage-php/src/Thing.php:61 Stack trace: #0 /nas/content/live/websitestg/wp-content/themes/astra-child/my-project/vendor/onpage-dev/onpage-php/src/Thing.php(35): OnPage\Thing->values('codice_paraf', NULL, Object(OnPage\Field)) #1 /nas/content/live/websitestg/wp-content/themes/astra-child/functions.php(322): OnPage\Thing->val('codice_paraf')`

the fields I use are single values, so I can't understand why it generates this

I also have a problem recovering the image fields, this error is generated:

Uncaught Error: Call to undefined method OnPage\Thing::file() in /nas/content/live/websitestg/wp-content/themes/astra-child/functions.php:320

Below is my complete code:

include_once get_stylesheet_directory() . '/my-project/vendor/autoload.php';

function update_post_from_api() {

$serializableData = [];
$fields = [];

$api_token;

$api = new \OnPage\Api('app', $api_token);

$prods = $api->query('prodotti')
    ->with([ 'articoli' ])
    ->all();

foreach ($prods as $prod) {

$codice_vision = $prod->val('codice_vision');
$subcategories = $prod->rel('articoli');

foreach ($subcategories as $subcategory) {

    //NO ERROR
    $prezzo = $subcategory->val('prezzo');

    //YES ERROR
    $codice_paraf = $subcategory->val('codice_paraf');
    $foto_sfondo_bianco = $subcategory->file('foto_sfondo_bianco')->link();

}

        $productData = [
            'codice_vision' => $prod->val('codice_vision'),
            'nome_prodotto' => $prod->val('nome_prodotto_nel_sito'),            
        ];

        $serializableData[$codice_vision] = $productData;

    }

}

Am I doing something wrong? To import the library I created a composer project which was then imported into the WordPress file system

gufoe commented 11 months ago

What version are you using, can you please try to install the latest version with:

composer require onpage-dev/onpage-php:^v1.1
saracomin96 commented 11 months ago

hi, thanks for the reply! I updated to the latest version but unfortunately the problem persists, what else could I check?

saracomin96 commented 11 months ago

I also tried locally with a composer project but it still doesn't work

gufoe commented 11 months ago

Can you send me the updated error backtrace? In your log i can see the error is at Thing.php(35), but that line contains a comment, not code, therefore it must refer to an older version.

saracomin96 commented 11 months ago

Fatal error: Uncaught TypeError: OnPage\Thing::values(): Return value must be of type Illuminate\Support\Collection, array returned in /nas/content/live/websitestg/wp-content/themes/astra-child/my-project/vendor/onpage-dev/onpage-php/src/Thing.php:61 Stack trace: #0 /nas/content/live/websitestg/wp-content/themes/astra-child/my-project/vendor/onpage-dev/onpage-php/src/Thing.php(35): OnPage\Thing->values('codice_paraf', NULL, Object(OnPage\Field)) #1 /nas/content/live/websitestg/wp-content/themes/astra-child/functions.php(323): OnPage\Thing->val('codice_paraf')

Same error, Am I doing something wrong in downloading the updated version of the library?

gufoe commented 11 months ago

I think so, can you try to remove and re-install the library?

composer remove onpage-dev/onpage-php
composer require onpage-dev/onpage-php:^v1.1
saracomin96 commented 11 months ago

Done but still the same error

Fatal error: Uncaught TypeError: OnPage\Thing::values(): Return value must be of type Illuminate\Support\Collection, array returned in C:\Users\Sara\Desktop\my_project\vendor\onpage-dev\onpage-php\src\Thing.php:61 Stack trace: #0 C:\Users\Sara\Desktop\my_project\vendor\onpage-dev\onpage-php\src\Thing.php(35): OnPage\Thing->values('codice_paraf', NULL, Object(OnPage\Field)) #1 C:\Users\Sara\Desktop\my_project\index.php(31): OnPage\Thing->val('codice_paraf') #2 {main} thrown in C:\Users\Sara\Desktop\my_project\vendor\onpage-dev\onpage-php\src\Thing.php on line 61

Thing.php(35) on this line there is this code

return $this->values($field_path, $lang, $field)->first();

complete function public function val(string $field_path, string $lang = null) //: null | string | bool | int | array | File { return $this->values($field_path, $lang, $field)->first(); }

saracomin96 commented 11 months ago

I managed to solve: I manually downloaded the folder from github and now it works, with the command the latest version is not downloaded

composer require onpage-dev/onpage-php:^v1.1

gufoe commented 11 months ago

What is your PHP version? Please send me an email at giacomo.rizzi@onpage.it and we'll follow the discussion there as there must be some error with your mistake.

Normally you should see you are installing version 1.1.79:

image

gufoe commented 11 months ago

Issue was resolved forcing composer to install the latest version. composer require onpage-dev/onpage-php:^v1.1.79