Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 9da76ea

Browse files
author
Nik Barham
committed
When setting a foreign property using the foreignkey_id -> foreignkey shortcut, ORM checks that the correct table has been passed.
More improved exception messages.
1 parent e659238 commit 9da76ea

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/Automatorm/Exception/Model.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ private function make_message($code, $data)
7373

7474
case 'MODEL_DATA:UNEXPECTED_COLUMN_NAME':
7575
list($model, $column, $value) = $data;
76-
return 'MODEL_DATA:UNEXPECTED_COLUMN_NAME: Property "'.$column.'" does not exist in the schema for this object ('.$model->table_name.'). Please check the $model for this object, or look at $obj->var_dump()';
76+
if ($model['columns'][$column . '_id']) {
77+
return 'MODEL_DATA:UNEXPECTED_COLUMN_NAME: Property "'.$column.'" does not exist in the schema for this object ('.$model['table_name'].'), but "'.$column.'_id" does. You probably haven\'t set up the foreign key for this column!';
78+
}
79+
return 'MODEL_DATA:UNEXPECTED_COLUMN_NAME: Property "'.$column.'" does not exist in the schema for this object ('.$model['table_name'].'). Please check the $model for this object, or look at $obj->var_dump()';
7780

7881
case 'MODEL_DATA_UPDATE:PIVOT_INCORRECT_OBJECT_TYPE':
7982
list($value, $table, $pivot) = $data;
@@ -84,9 +87,12 @@ private function make_message($code, $data)
8487
list($column, $value) = $data;
8588
return 'MODEL_DATA:CANNOT_CALL_MULTIPIVOT_AS_PROPERTY: Property "'.$column.'" represents a M-M (Pivot) relationship with more than two keys. As we don\'t know which type of object to return (as there are multiple choices), you can\'t call this link as a simple property. Use the $model->property([\$where]) syntax instead.';
8689

87-
case 'MODEL_DATA:CANNOT_DELETE_UNCOMMITED_DATA';
90+
case 'MODEL_DATA:CANNOT_DELETE_UNCOMMITED_DATA':
8891
return 'MODEL_DATA:CANNOT_DELETE_UNCOMMITED_DATA: You cannot mark a Data object for deletion if it does not represent an existing row in the database.';
8992

93+
case 'MODEL_DATA:INCORRECT_MODEL_FOR_RELATIONSHIP':
94+
list($column, $supplied_table, $expected_table) = $data;
95+
return 'MODEL_DATA:INCORRECT_MODEL_FOR_RELATIONSHIP: Property "'.$column.'" expected a Model relating to table "'.$expected_table.'" but a Model for "'.$supplied_table.'" was given instead.';
9096
default:
9197
return "Unknown error code ({$code})";
9298
}

src/Automatorm/Orm/Data.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,16 @@ public function __set($var, $value)
581581
$this->external[$var] = null;
582582
return;
583583
} elseif ($value instanceof Model) {
584+
// Trying to pass in the wrong table for the relationship!
585+
// That is, the table name on the foreign key does not match the table name in the passed Model object
586+
if ($value->data(true)->table !== $this->schema->getTable($var)['table_name']) {
587+
throw new Exception\Model('MODEL_DATA:INCORRECT_MODEL_FOR_RELATIONSHIP', [$var, $value->table, $this->schema->model[$var]['table_name']]);
588+
}
584589
$this->data[$var.'_id'] = $value->id;
585590
$this->external[$var] = $value;
586591
return;
587592
} else {
588-
throw new Exception\Model('MODEL_DATA:MODEL_EXPECTED_FOR_KEY', array($var, $value));
593+
throw new Exception\Model('MODEL_DATA:MODEL_EXPECTED_FOR_KEY', [$var, $value]);
589594
}
590595
}
591596

src/Automatorm/Orm/Schema.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,21 @@ public static function generateSchema($dbconnection)
191191
private static $stringCacheN = [];
192192
public static function normaliseCase($string)
193193
{
194-
if (static::$stringCacheN[$string]) return static::$stringCacheN[$string];
194+
if (isset(static::$stringCacheN[$string])) return static::$stringCacheN[$string];
195195
return static::$stringCacheN[$string] = trim(strtolower(preg_replace('/([A-Z])|_/', ' $1', $string)));
196196
}
197197

198198
private static $stringCacheC = [];
199199
public static function camelCase($string)
200200
{
201-
if (static::$stringCacheC[$string]) return static::$stringCacheC[$string];
201+
if (isset(static::$stringCacheC[$string])) return static::$stringCacheC[$string];
202202
return static::$stringCacheC[$string] = str_replace(' ', '', ucwords(self::normaliseCase($string)));
203203
}
204204

205205
private static $stringCacheU = [];
206206
public static function underscoreCase($string)
207207
{
208-
if (static::$stringCacheU[$string]) return static::$stringCacheU[$string];
208+
if (isset(static::$stringCacheU[$string])) return static::$stringCacheU[$string];
209209
return static::$stringCacheU[$string] = str_replace(' ', '_', self::normaliseCase($string));
210210
}
211211

@@ -214,7 +214,7 @@ public static function underscoreCase($string)
214214
public function guessContext($class_or_table)
215215
{
216216
// Return 'cached' result
217-
if (static::$contextCache[$class_or_table]) return static::$contextCache[$class_or_table];
217+
if (isset(static::$contextCache[$class_or_table])) return static::$contextCache[$class_or_table];
218218

219219
// Namespace classname? Remove that namespace before continuing
220220
if (strrpos($class_or_table, '\\') !== false) {
@@ -242,7 +242,7 @@ public function guessContext($class_or_table)
242242

243243
// We haven't found an entry in the schema for our best guess table name? Boom!
244244
if (!$table) {
245-
throw new Exception\Model('NO_SCHEMA', [$class_or_table, $normalised, $class, $table]);
245+
throw new Exception\Model('NO_SCHEMA', [$class_or_table, $normalised, $class]);
246246
}
247247

248248
return static::$contextCache[$class_or_table] = [$class, $table];

0 commit comments

Comments
 (0)