4spacesdk / CI4OrmExtension

OrmExtension for CodeIgniter 4
MIT License
50 stars 9 forks source link

Error: __clone method called on non-object #5

Closed Schop closed 4 years ago

Schop commented 4 years ago

When using the asArray() method of a model, I get the following error:

Error

__clone method called on non-object

C:\xampp\htdocs\euro2021\vendor\4spacesdk\ci4ormextension\Extensions\Model.php at line 469

462         if($result instanceof Entity)
463             $result = [$result];
464 
465         $this->arrangeIncludedRelations($result);
466 
467         // Convert from array to single Entity
468         if(is_array($result) && count($result) > 0) {
469             $first = clone $result[0];
470             foreach($result as $item) $first->add($item);
471             $result = $first;
472         } else {
473             $result = new $this->returnType();
474         }
475 
476         $data['data'] = $result;
Martin-4Spaces commented 4 years ago

Hmm, maybe I should add support for the asArray method. I think it is equivalent to find() and then call toArray() or allToArray() for multiple results.

$userModel = new UserModel();
$users = $userModel->find(); 
echo json_encode($users->allToArray());

Maybe you can show your use case.

Schop commented 4 years ago

Aha, I overlooked the allToArray() method. This will serve my purposes just fine. However, it would be nice if all the native methods of the CodeIgniter model would be supported.

Martin-4Spaces commented 4 years ago

Agree and noted.