From 2f5ba2af7af5c53ef62b3ca4b75a70d39ff010d5 Mon Sep 17 00:00:00 2001 From: Rougin Gutib Date: Sun, 13 Oct 2024 22:39:39 +0800 Subject: [PATCH] Rework Repo template for booleans --- src/Template/Repository.php | 14 +++++++------- tests/Fixture/Plates/Doctrine/Repos/Post.php | 6 +++--- tests/Fixture/Plates/Doctrine/Repos/User.php | 15 +++++++++------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Template/Repository.php b/src/Template/Repository.php index 673835a..1d7c373 100644 --- a/src/Template/Repository.php +++ b/src/Template/Repository.php @@ -160,19 +160,19 @@ protected function setSetMethod($table) $type = $type . '|null'; } - $lines[] = '/** @var ' . $type . ' */'; - $lines[] = '$' . $name . ' = $data[\'' . $name . '\'];'; - $isNull = $col->isNull(); - $space = $isNull ? ' ' : ''; + $space = $isNull || $type === 'boolean' ? ' ' : ''; - if ($isNull) + if ($isNull || $type === 'boolean') { - $lines[] = 'if ($' . $name . ')'; + $lines[] = 'if (array_key_exists(\'' . $name . '\', $data))'; $lines[] = '{'; } + $lines[] = $space . '/** @var ' . $type . ' */'; + $lines[] = $space . '$' . $name . ' = $data[\'' . $name . '\'];'; + if ($col->isForeignKey()) { $foreign = $col->getReferencedTable(); @@ -187,7 +187,7 @@ protected function setSetMethod($table) $lines[] = $space . '$entity->set_' . $name . '($' . $name . ');'; - if ($isNull) + if ($isNull || $type === 'boolean') { $lines[] = '}'; } diff --git a/tests/Fixture/Plates/Doctrine/Repos/Post.php b/tests/Fixture/Plates/Doctrine/Repos/Post.php index 4785a61..b7dff0a 100644 --- a/tests/Fixture/Plates/Doctrine/Repos/Post.php +++ b/tests/Fixture/Plates/Doctrine/Repos/Post.php @@ -49,10 +49,10 @@ public function set($data, $entity, $id = null) $text = $data['text']; $entity->set_text($text); - /** @var integer|null */ - $user_id = $data['user_id']; - if ($user_id) + if (array_key_exists('user_id', $data)) { + /** @var integer|null */ + $user_id = $data['user_id']; $user = $this->_em->find('User', $user_id); $entity->set_user($user); } diff --git a/tests/Fixture/Plates/Doctrine/Repos/User.php b/tests/Fixture/Plates/Doctrine/Repos/User.php index aa8b8ce..44dff3c 100644 --- a/tests/Fixture/Plates/Doctrine/Repos/User.php +++ b/tests/Fixture/Plates/Doctrine/Repos/User.php @@ -53,14 +53,17 @@ public function set($data, $entity, $id = null) $year = $data['year']; $entity->set_year($year); - /** @var boolean */ - $admin = $data['admin']; - $entity->set_admin($admin); + if (array_key_exists('admin', $data)) + { + /** @var boolean */ + $admin = $data['admin']; + $entity->set_admin($admin); + } - /** @var string|null */ - $remarks = $data['remarks']; - if ($remarks) + if (array_key_exists('remarks', $data)) { + /** @var string|null */ + $remarks = $data['remarks']; $entity->set_remarks($remarks); } // -----------------------------------