From 8b25925ec473230bf272b6f26aef99c2111eb1f0 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Thu, 23 May 2019 17:52:11 +0200 Subject: [PATCH] Successfully tested create, update and delete of trivial elements --- src/admin/common/abstract/model.php | 16 +++++++++++++--- src/admin/common/abstract/model/factory.php | 4 ++-- src/admin/common/models/place.php | 6 ++++++ src/admin/views/test/tmpl/default.php | 3 +++ src/admin/views/test/view.html.php | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/admin/common/abstract/model.php b/src/admin/common/abstract/model.php index 1b1dab8..3ecb62b 100644 --- a/src/admin/common/abstract/model.php +++ b/src/admin/common/abstract/model.php @@ -6,6 +6,9 @@ use Joomla\CMS\Factory; // No direct access. defined('_JEXEC') or die; +class AssociatedObjectUnsavedException extends Exception +{} + abstract class AbstractCommonClubsModel { // TODO Adddata validator @@ -27,9 +30,9 @@ abstract class AbstractCommonClubsModel private $values = null; - protected function getValues() + protected function getValues($force = false) { - if(is_null($this->values)) + if(is_null($this->values) || $force) $this->loadDataFromDatabase(); return $this->values; @@ -128,7 +131,14 @@ abstract class AbstractCommonClubsModel if(is_null($vals[$k])) continue; - $vals[$k] = $vals[$k]->getId(); + $id = $vals[$k]->getId(); + + if($id === 'new') + { + throw new AssociatedObjectUnsavedException(); + } + + $vals[$k] = $id; } return $vals; diff --git a/src/admin/common/abstract/model/factory.php b/src/admin/common/abstract/model/factory.php index bb60ee9..baccce6 100644 --- a/src/admin/common/abstract/model/factory.php +++ b/src/admin/common/abstract/model/factory.php @@ -36,9 +36,9 @@ abstract class AbstractCommonClubsModelFactory public abstract function getAttributes(); private $attributes = null; - private function fetchAttributes() + private function fetchAttributes($force = False) { - if($this->attributes === null) + if($this->attributes === null || $force) $this->attributes = $this->getAttributes(); return $this->attributes; diff --git a/src/admin/common/models/place.php b/src/admin/common/models/place.php index f64d134..0b55d8b 100644 --- a/src/admin/common/models/place.php +++ b/src/admin/common/models/place.php @@ -34,4 +34,10 @@ class CommonClubsModelPlace extends AbstractCommonClubsModel { $this->setValue('area', $area); } + + public function setClub($club) + { + $this->setValue('club', $club); + } + } \ No newline at end of file diff --git a/src/admin/views/test/tmpl/default.php b/src/admin/views/test/tmpl/default.php index 18076f7..90b91bb 100644 --- a/src/admin/views/test/tmpl/default.php +++ b/src/admin/views/test/tmpl/default.php @@ -17,3 +17,6 @@ ClubName: club->getPresident()->getName(); ?>
club->getPlaces() as $place): ?> Place: getName(); ?>
+ +

Output

+
log); ?>
\ No newline at end of file diff --git a/src/admin/views/test/view.html.php b/src/admin/views/test/view.html.php index 75186ca..23694b4 100644 --- a/src/admin/views/test/view.html.php +++ b/src/admin/views/test/view.html.php @@ -14,6 +14,8 @@ class ClubsViewTest extends HtmlView { ToolbarHelper::title('Test'); + $this->log = ''; + $factory = new CommonClubsModelFactoryClub(); $this->clubs = $factory->loadElements(); @@ -27,6 +29,20 @@ class ClubsViewTest extends HtmlView // $places[0]->setName("abc"); // $places[0]->save(); + $pfactory = new CommonClubsModelFactoryPlace(); + $np = $pfactory->createNew(); + $np->setName('MyName'); + $np->setClub($c); + //$np->save(); + $np->getId(); + + $np = $c->getPlaces()[1]; + $np->getName(); + $np->setName('foo2 with new Name'); +// $np->save(); +// $np->delete(); + $this->log = $np; + parent::display($tpl); }