Skip to content

Commit

Permalink
add upgrade exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 27, 2024
1 parent 41d5803 commit 1a6f47f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ to populate a content:
```
$grid->addModalAction('Details', 'Additional Details', function (View $p, $id) use ($grid) {
// $id of the record which was clicked
// $grid->model = $grid->model->load($id);
// $model = $grid->model->load($id);
LoremIpsum::addTo($p);
});
Expand Down
2 changes: 1 addition & 1 deletion src/Form/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function _addControl(Control $control, Field $field): Control
public function addControl(string $name, $control = [], array $fieldSeed = []): Control
{
if ($this->form->entity === null) {
$this->form->entity = (new ProxyModel())->createEntity();
$this->form->setModel((new ProxyModel())->createEntity());
}
$model = $this->form->entity->getModel();

Expand Down
2 changes: 1 addition & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function addColumn(?string $name, $columnDecorator = [], $field = [])
}

if ($this->model === null) {
$this->model = new ProxyModel();
$this->setModel(new ProxyModel());
}
$this->model->assertIsModel();

Expand Down
15 changes: 13 additions & 2 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ public function __construct($label = [])
$this->setDefaults($defaults);
}

#[\Override]
public function &__get(string $name)
{
// TODO remove in atk4/ui 6.0
if ($name === 'model' && !(new \ReflectionProperty(self::class, 'model'))->isInitialized($this) && $this->entity !== null) {
throw new Exception('Use View::$entity property instead for entity access');
}

return parent::__get($name);
}

/**
* Associate this view with a model. Do not place any logic in this class, instead take it
* to renderView().
Expand All @@ -113,8 +124,8 @@ public function __construct($label = [])
*/
public function setModel(Model $model): void
{
if ($this->model !== null) {
if ($this->model === $model) {
if (((new \ReflectionProperty(self::class, 'model'))->isInitialized($this) ? $this->model : $this->entity) !== null) {
if (((new \ReflectionProperty(self::class, 'model'))->isInitialized($this) ? $this->model : $this->entity) === $model) {
return;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Atk4\Ui\Callback;
use Atk4\Ui\Console;
use Atk4\Ui\Exception;
use Atk4\Ui\Form;
use Atk4\Ui\JsCallback;
use Atk4\Ui\JsSse;
use Atk4\Ui\Loader;
Expand Down Expand Up @@ -103,6 +104,22 @@ public function testSetModelTwiceException(): void
$v->setModel($m2);
}

public function testSetModelEntity(): void
{
$form = new Form();
$form->setApp($this->createApp());
$form->invokeInit();
$entity = (new Model())->createEntity();
$form->setModel($entity);

self::assertSame($entity, $form->entity);
self::assertFalse((new \ReflectionProperty(Form::class, 'model'))->isInitialized($form));

$this->expectException(Exception::class);
$this->expectExceptionMessage('Use View::$entity property instead for entity access');
$form->model; // @phpstan-ignore-line
}

public function testSetSourceZeroKeyException(): void
{
$v = new View();
Expand Down

0 comments on commit 1a6f47f

Please sign in to comment.