Problem with update, writeContent for page to SQLite

I am having trouble with update and writeContent. I have a controller that calls $page ->update with a few fields that need to be updated.
In the pages model I have a writeContent function that updates the database as shown in the database examples.

Writing to the database works but results in any of the fields that were not set through the update function to be empty.

The page object does have the correct data when I dump it from the writeContent method but the data object that the function receives has all fields and the ones that are not sent to the update function are all empty strings.

so If I do this:

$page->update([‘videoid’=>‘12345’], $language->code());

the data the writeContent method receives is this:

array(12) {
[“title”]=>
string(0) “”
[“videoid”]=>
string(5) “12345”
[“description”]=>
string(0) “”
[“public”]=>
string(5) “false”
[“count”]=>
NULL
[“language”]=>
string(2) “nl”
[“owner”]=>
string(0) “”
[“timestamp”]=>
string(0) “”
}

This erases all the existing data (except the updated field). I must be doing something wrong but I can’t figure out why the other fields are being set to empty strings.

this is the writeContent method I am using:

public function writeContent(array $data, string $languageCode = null): bool
{
    if ($video = Db::first('videos', '*', ['slug' => $this->slug(), 'language' => $languageCode])) {
        $result =  Db::update('videos', $data, ['slug' => $this->slug(), 'language' => $languageCode]);      
    } else {
        $data['language'] = $languageCode;
        $data['slug'] = $this->slug();

        $result = Db::insert('videos', $data);
    }
    return $result;

}

The example setup is only for a single language site but you seem to be in a multi-language context. Therefore Kirby doesn’t fetch the correct data set before saving. What does your model look like?

Have you defined the translations?

Ah that makes sense.
I have this in the model of the parent page:

class VideosPage extends Kirby\Cms\Page
{
    public function children()
    {
        $videos = [];
        foreach (Db::select('videos','*','language = "'.kirby()->language().'"') as $video) {
            $videos[] = [
                'slug'     => $video->slug(),
                'num'      => 0,
                'template' => 'video',
                'model'    => 'video',
                'content'  => [
                    'title'           => $video->title(),
                    'owner'           => $video->owner(),
                    
                    'title'             => $video->title(),
                    'videoid'           => $video->videoid(),
                    'public'            => $video->public(),
                    'category'          => $video->category(),
                    'count'             => $video->count(),
                    'language'          => $video->language(),
                    'description'       => $video->description(),
                    'timestamp'         => $video->created(),
  
                ]
            ];
        }

        return Pages::factory($videos, $this);
    }
}

Do I need to have something similar in the child page model? I ask because displaying the data from the database is working fine without it.

No, it’s not about the child model, but about defining the translations instead of the content array, like in the example I linked to. You can also see it at work with more detail in my unpublished “users from database” recipe, although it is all about user content, but the principle is the same:

from line 960ff

I’ve always wanted to extend the Virtual Pages guide, but didn’t get round to it yet.

Ah, okay. I think I understand.

In multi language sites the model doesn’t return

$videos[] = [
                'slug'     => $video->slug(),
                'num'      => 0,
                'template' => 'video',
                'model'    => 'video',
                'content'  => [
                    'title'           => $video->title(),
                    'owner'           => $video->owner(),
                    'videoid'           => $video->videoid(),
                    'public'            => $video->public(),
                    'category'          => $video->category(),
                    'count'             => $video->count(),
                    'language'          => $video->language(),
                    'description'       => $video->description(),
                    'timestamp'         => $video->created(),
                  ]
            ];

but rather:

$videos[] = [
                'slug'     => $video->slug(),
                'num'      => 0,
                'template' => 'video',
                'model'    => 'video',
                'translations' => [
                             [
                                 'code' => 'en',
                                 'content' => $content_en
                            ],
                            [
                                 'code' => 'nl',
                                 'content' => $content_nl,
                           ],
    ],
];

Thank you very much.

Please don’t use quotes to post code, but wrap your code in three backticks on separate lines, like this to make it readable. Thanks!

three-backticks

Somebody should add a button for that :slight_smile:

Yes, probably, but even on GitHub they don’t have such a button.