From 1b0a9f385f542167a405f70412efe579ba5e213e Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Mon, 16 Feb 2026 13:37:52 +0100 Subject: [PATCH] fix: use `AUTO_INCREMENT` of table as id of new posts (#9) Reviewed-on: https://codeberg.org/vlw/wp/pulls/9 --- src/Posts/Post.php | 5 ++++- src/Posts/PostMeta.php | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Posts/Post.php b/src/Posts/Post.php index 9ff9d9e..710b0fa 100644 --- a/src/Posts/Post.php +++ b/src/Posts/Post.php @@ -47,6 +47,9 @@ return self::from_name($title); } + // Current auto increment value will be the id for this entity + $id = Database::current()->latest(Posts::TABLE_NAME); + $values = [ Posts::ID->value => null, Posts::POST_AUTHOR->value => 0, @@ -77,7 +80,7 @@ throw new Exception("Failed to create database entity"); } - return self::from_name($title); + return new static($id); } public function __construct(public readonly int $id) { diff --git a/src/Posts/PostMeta.php b/src/Posts/PostMeta.php index 0a8ebf1..190546b 100644 --- a/src/Posts/PostMeta.php +++ b/src/Posts/PostMeta.php @@ -64,6 +64,9 @@ return $model; } + // Current auto increment value will be the id for this entity + $id = Database::current()->latest(PostMetaTable::TABLE_NAME); + $values = [ PostMetaTable::META_ID->value => null, PostMetaTable::POST_ID->value => $post->id, @@ -75,7 +78,7 @@ throw new Exception("Failed to create database entity"); } - return new static(self::get_post_meta($post, $meta_key)->id); + return new static($id); } public function __construct(public readonly int $id) {