Many authors-to-article relationship: is this a good solution?

I’m new to Kirby and working on a research-focused site where I need to have a many-to-many-style relationship between authors and articles, like this:

I’ve come up with a solution (described below), but my question is basically: is this a good solution? Are there pitfalls/issues I’m not seeing? Is it cool to use uuids to reference things like this?

Here’s what I did:

  • Articles have a multiselect field that displays the author names as options, and saves their uuid to the article
  • Author pages display articles of posts if they contain the author’s uuid
  • When an author is deleted, it triggers a hook (see below) to remove the author’s uuid from all articles
        'page.delete:after' => function ($page) {

            if ($page->template() == "author") {

                $articles = page('articles')->children()->filterBy('auths', $page->uuid(), ',');

                foreach ($articles as $article) {

                    $auths = $article->auths()->split(',');

                    if (($key = array_search($page->uuid(), $auths)) !== false) {
                        unset($auths[$key]);
                    }

                    $updatedAuths = implode(',',$auths);

                    try {

                        $newPage = page($article->uuid())->update([
                            'auths' => $updatedAuths
                        ]);
                    
                        error_log('The author has been removed.');
                    
                    } catch(Exception $e) {
                    
                        $outputMessage = "From author hook:" . $e->getMessage();
                        error_log($outputMessage);
                    
                    }

                }

            }

        }

I’d prefer a users field, but other than that, yes, only assigning authors to articles and then get the relevant articles for the author by filtering makes absolute sense.

Not sure if the hook is really needed. Yes, it cleans up the content files, but if an author doesn’t exist anymore, they wouldn’t show up anyway when converted to pages ($field->toPages()).

Thank you for your feedback! I hadn’t thought there would be a way to just leave the deleted author uuids in the article content without causing issue. toPages() sounds like a much simpler solution than the hook.

Can you explain how to use toPages() on a multiselect value?

My content gets stored like this on the article:

Auths: page://zL0BZ2p7Ais2uKGf, page://mThtswO6nlNLXwh1

When I do $page->auths()->toPages(); it doesn’t return anything.

When I do var_dump($page->auths()) the field value is a comma separated string of the uuids.

I see the doc example is using content formatted like a yaml list. What am I missing? :slight_smile:

That refers to using a pages field instead of a multiselect. With a comma separated list of page uuids, you have to pass a comma as params to toPages(),:

$page->auths()->toPages(',');

Ah! Is see the answer is right there on the page I linked to. :person_facepalming:

Can be any other separator to split the field value by

Ty!