@@ -183,7 +183,7 @@ class Datagrid extends Control
183183 /** @var array<ToolbarButton> */
184184 protected array $ toolbarButtons = [];
185185
186- protected ?DataModel $ dataModel = null ;
186+ protected ?AbstractDataModel $ dataModel = null ;
187187
188188 protected string $ primaryKey = 'id ' ;
189189
@@ -344,7 +344,7 @@ public function render(): void
344344 /**
345345 * Check whether datagrid has set some columns, initiated data source, etc
346346 */
347- if (!($ this ->dataModel instanceof DataModel )) {
347+ if (!($ this ->dataModel instanceof AbstractDataModel )) {
348348 throw new DatagridException ('You have to set a data source first. ' );
349349 }
350350
@@ -469,7 +469,7 @@ public function setRowCallback(callable $callback): self
469469 */
470470 public function setPrimaryKey (string $ primaryKey ): self
471471 {
472- if ($ this ->dataModel instanceof DataModel ) {
472+ if ($ this ->dataModel instanceof AbstractDataModel ) {
473473 throw new DatagridException ('Please set datagrid primary key before setting datasource. ' );
474474 }
475475
@@ -481,10 +481,18 @@ public function setPrimaryKey(string $primaryKey): self
481481 /**
482482 * @return static
483483 * @throws InvalidArgumentException
484+ * @throws DatagridException
484485 */
485486 public function setDataSource (mixed $ source ): self
486487 {
487- $ this ->dataModel = new DataModel ($ source , $ this ->primaryKey );
488+ $ this ->setDataModel (new DataModel ($ source , $ this ->primaryKey ));
489+
490+ return $ this ;
491+ }
492+
493+ public function setDataModel (AbstractDataModel $ dataModel ): self
494+ {
495+ $ this ->dataModel = $ dataModel ;
488496
489497 $ this ->dataModel ->onBeforeFilter [] = [$ this , 'beforeDataModelFilter ' ];
490498 $ this ->dataModel ->onAfterFilter [] = [$ this , 'afterDataModelFilter ' ];
0 commit comments