public function __set($field, $value) { $this->_checkFieldExists($field); // if you try to change the primary key // check if another record exists $prim = self::$_primaryField; if ($field == $prim && $value != $this->_id) { $valueEsc = $this->_db->esc($value); $sql = "SELECT `{$prim}` AS id FROM `{$this->_table}` WHERE `{$prim}` = '{$valueEsc}' "; $res = $this->_db->executeSQL($sql); if (!$res) { throw new ModelException("Invalid query - " . $this->_db->getError()); } $res = $this->_db->getResultset(); if (count($res) > 0) { throw new ModelException("Table {$this->_table} already has record with {$prim} = {$value}"); } } // proceed with setting the new value $fieldType = self::$_metadata[$field]; if (substr($fieldType, 0, 3) == 'int') { $this->_data[$field] = (int) $value; } else { $this->_data[$field] = $value; } }