From 6a9e1606387f5315da8bf18960a907bbb68e903e Mon Sep 17 00:00:00 2001 From: Toby Allen Date: Fri, 4 Oct 2024 21:52:40 +0100 Subject: [PATCH 1/2] check attributes array --- src/AnyModel.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/AnyModel.php b/src/AnyModel.php index 1e89481..66745a3 100644 --- a/src/AnyModel.php +++ b/src/AnyModel.php @@ -26,12 +26,14 @@ public static function table($tablename, $options = []){ return $Model; } public function getIDattribute(){ - // due to various contortions Model->id will not always be available and it is not possible to set + + // due to various contortions Model->id will not always be available. If it is not set via // primaryKey and have it work so if it is requested and does not exist rather than returning '' (default behaviour) // raise an error. - if (!isset($this->{$this->primaryKey})){ + if (!isset($this->attributes[$this->primaryKey])){ throw new \Exception('AnyModel::id does not have a value. Please specify actual id field name'); } + return $this->attributes[$this->primaryKey]; } From 414d4a96eb01dc8f9884318ba540697a7d68dedf Mon Sep 17 00:00:00 2001 From: Toby Allen Date: Fri, 4 Oct 2024 21:59:34 +0100 Subject: [PATCH 2/2] comments --- src/AnyModel.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/AnyModel.php b/src/AnyModel.php index 66745a3..5323a36 100644 --- a/src/AnyModel.php +++ b/src/AnyModel.php @@ -12,6 +12,12 @@ class AnyModel extends Model public $timestamps = false; protected $guarded = []; + /** + * Set the tablename that the model will be loaded from. + * @param string $tablename + * @param array $options + * @return AnyModel + */ public static function table($tablename, $options = []){ // Create a new Model and set its table @@ -19,20 +25,32 @@ public static function table($tablename, $options = []){ $Model->table = $tablename; // Set any further options passed in before returning + // since AnyModel extends Model protected properties + // on Model can be set here. Collect($options)->each(function($value, $name) use ($Model){ $Model->{$name} = $value; }); return $Model; } + + /** + * If used on table without id field, Model->id will not be available. If it is not set via + * primaryKey and have it work so if it is requested and does not exist rather than + * returning '' (default behaviour) raise an error. + * @return mixed + * @throws \Exception + */ public function getIDattribute(){ // due to various contortions Model->id will not always be available. If it is not set via - // primaryKey and have it work so if it is requested and does not exist rather than returning '' (default behaviour) - // raise an error. + // primaryKey and have it work so if it is requested and does not exist, rather than + // returning '' (default behaviour), raise an error. if (!isset($this->attributes[$this->primaryKey])){ throw new \Exception('AnyModel::id does not have a value. Please specify actual id field name'); } + + // return primary key value. return $this->attributes[$this->primaryKey]; }