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'  => '',
        ]);

2 Likes

Thanks a lot for posting the solution @duncanmunro , i was struggling with the same thing. Now it works and i can adapt to my needs.