Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

AUTOID doesn't take non-existing ids into account #50

Open
nilshoerrmann opened this issue Aug 6, 2020 · 2 comments
Open

AUTOID doesn't take non-existing ids into account #50

nilshoerrmann opened this issue Aug 6, 2020 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@nilshoerrmann
Copy link

I have a model in which I use AutoID to create custom page URLs. Simplified, it looks like this (the actualy URL is more complex):

<?php

class EventPage extends Page
{
    public function url($options = null): string
    {
        $url = $this->parent()->url() . '/' . $this->AUTOID();
        return Url::to($url, $options);
    }
}

When creating a page AutoID throws an error because it's trying to call autoid on null here:
https://github.com/bnomei/kirby3-autoid/blob/master/index.php#L82

This happens because we are a step before the AutoID is actually created. I'm not sure about the correct behaviour in general. But in my case I'd like AutoID so either return an empty string or null.

@bnomei bnomei self-assigned this Aug 10, 2020
@bnomei bnomei added the question Further information is requested label Aug 10, 2020
@bnomei
Copy link
Owner

bnomei commented Aug 10, 2020

i am not sure exactly why $this is null in that case but you need to check in you code if the object you are calling autoid on exists.

public function url($options = null): string
    {
       // QUESTION: is $this null here as well?
        $url = $this->parent()->url() . '/' . $this->AUTOID();
        return Url::to($url, $options);
    }

why not do this? it will be valid once the id exists.

public function url($options = null): string
    {
        $url = $this->parent()->url() . '/' . ($this ? $this->AUTOID(): '');
        return Url::to($url, $options);
    }

have you tried using the lowercase version $this->autoid() that avoid trying to push the page and just reads the field.

@nilshoerrmann
Copy link
Author

nilshoerrmann commented Aug 10, 2020

have you tried using the lowercase version $this->autoid() that avoid trying to push the page and just reads the field.

Oh, yes, I do work around this in my code like this:

// Make sure there is actually an autoid stored already
if ($this->autoid()) {
    $url = $this->parent()->url() . '/' . $date . '/' . $this->AUTOID();
}

I just wanted to notify you that this is happening.

// QUESTION: is $this null here as well?

No, it's not, it's a valid page object.

i am not sure exactly why $this is null in that case

From what I understand, $this->id() is not set yet when creating a page and thus there is nothing that's returned from the AutoID DB (null) and autoid() is called internally on that null value:

$db->findByID($this->id())->autoid();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants