Skip to content

Commit

Permalink
Merge pull request #1 from tobya/allowidviaattribute
Browse files Browse the repository at this point in the history
Allow id via attribute
  • Loading branch information
tobya authored Oct 4, 2024
2 parents 63e64c9 + 414d4a9 commit a09b954
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/AnyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,47 @@ 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
$Model = new AnyModel();
$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 and it is not possible to set
// 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})){

// 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->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];

}

}

0 comments on commit a09b954

Please sign in to comment.