Skip to content

Commit 45dec3b

Browse files
author
Toby Allen
committed
Raise exception if id not available
1 parent 2a3225d commit 45dec3b

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/AnyModel.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@
88

99
class AnyModel extends Model
1010
{
11-
11+
1212
public $timestamps = false;
1313

14-
public static function table($tablename, $idfield = null, $options = []){
15-
14+
15+
public static function table($tablename, $options = []){
16+
1617
// Create a new Model and set its table and primary key.
1718
$Model = new AnyModel();
18-
$Model->table = $tablename;
19-
$Model->primaryKey = $idfield;
20-
21-
// Set any further options passed in before returning
22-
Collect($options)->each(function($value, $name){
23-
$this->{$name} = $value;
24-
});
25-
19+
$Model->table = $tablename;
20+
21+
// Set any further options passed in before returning
22+
Collect($options)->each(function($value, $name) use ($Model){
23+
$Model->{$name} = $value;
24+
});
25+
2626
return $Model;
2727
}
28-
29-
}
28+
public function getIDattribute(){
29+
// due to various contortions Model->id will not always be available and it is not possible to set
30+
// primaryKey and have it work so if it is requested and does not exist rather than returning '' (default behaviour)
31+
// raise an error.
32+
if (!isset($this->{$this->primaryKey})){
33+
throw new \Exception('AnyModel::id does not have a value. Please specify actual id field name');
34+
}
35+
36+
}
37+
38+
}

0 commit comments

Comments
 (0)