prismicio-community / php-kit

Community maintained development kit for Prismic and the PHP language
https://prismic.io
Other
109 stars 83 forks source link

GetByID returning NULL #197

Closed pTinosq closed 1 year ago

pTinosq commented 1 year ago

I'm trying to use the GetByID function with the ID "project" to fetch all my projects as shown in the screenshots below, however, it's always returning NULL. When I try to input an ID that does not exist, it returns:

{"page":1,"results_per_page":20,"results_size":0,"total_results_size":0,"total_pages":0,"next_page":null,"prev_page":null,"results":[],"version":"0427058","license":"All Rights Reserved"}"

This has led me to believe that it is finding the documents with the correct IDs, however, for some reason something's causing it to return NULL instead of the appropriate data.

image image

Here is the code I am using:


include_once 'vendor/autoload.php';

use Prismic\Api;

$url = 'https://' . env('PRISMIC_NAME') . '.prismic.io/api/v2';
$api = Api::get($url, env('PRISMIC_TOKEN'));

$projects = $api->getByID('project');

var_dump($projects); // NULL 
pTinosq commented 1 year ago

I've found the following workaround for the time being:

$api->query(
    Prismic\Predicates::at('document.type', 'project'),
    [
        'orderings' => '[my.project.date desc]',
        'pageSize' => 100,
    ]
);
c0nst4ntin commented 1 year ago

@pTinosq As stated in the Documentation of getByID the function takes the document ID as a parameter. Every document is automatically assigned a unique id when it is created.

I believe you might have confused the custom type with the document id here. Is that possible? Your workaround seems to be using the correct function for this type of query. You can find more on this here: https://github.com/prismicio-community/php-kit/blob/master/docs/02-query-the-api/02-query-predicate-reference.md

c0nst4ntin commented 1 year ago

Has this helped you? Or do you still experience any problems?

If this is solved I would like to close this issue

pTinosq commented 1 year ago

Sorry for not replying, yeah all's well - thank you for clearing that up.