Virtual pages from database question

writeContent() isn’t the right method to overwrite when creating a new page via the Panel, that would be the create method in the parent model.

But you said you tested Db::insert() also in a template and that didn’t work? That would be strange.

oh… ok.
I can’t see any create method in the parent (comments) model.

The if statement in the comment model seemed like the only place where it might happen. I’m learning as I go as pretty much everything mysql is new to me. I assumed the Db::insert() would be like inserting a row in phpMyAdmin. In my thinking (though of course I likely wrong) it first checks if slug exists and if so Db::update, otherwise Db::insert

But even if I copy that whole example from the docs in its fullest ‘advanced’ form it will not create a new child page (regardless of where I might think the creation happens).

This is the code I ran from the template as a test trying to create a new row (which it didn’t)

	$test = Db::insert('comments', [
	'slug' => 'new-slug',
    'text' => 'New comment text',
    'user' => 'Bob',
]);

Because the case that you create comments from the Panel is not covered in the recipe.

I see. Thanks, that helps.
The parent model needs a create method and that method needs to use… the best one I can see is Db::execute() ? (“Executes a raw SQL query which expects no set of results (i.e. update, insert, delete”)
So i need to figure out what a create method, and a query, looks like and how to insert a row with one? A little out of my depth but I can try working through that.
Thanks for your help.

In case anyone’s interested – after some experimenting it seems the example in docs DOES create new comments/database rows and it does do it in the child (comment) model. What I had to do to make it work was explicitly include all table’s columns in the DB::insert statement.

So I used this code instead of the lines in the pink box in the pic below:

            return Db::insert('comments', [
            'slug'  => $this->slug(),
            'text'  => '',
            'user'  => '',
        ]);

1 Like