/** * Insert or Update a record in a database table * @return inte * @return exception */ public function save() { try { $dbConn = self::makeDbConn(); if (isset($this->record['dbData']) && is_array($this->record['dbData'])) { $sql = 'UPDATE ' . $this->tableName() . ' SET ' . Formatter::tokenize(implode(',', Formatter::makeAssociativeArray($this->record)), ',') . ' WHERE id=' . $this->record['dbData']['id']; $query = $dbConn->prepare($sql); $query->execute(); } else { $sql = 'INSERT INTO ' . $this->tableName() . ' (' . Formatter::tokenize(implode(',', array_keys($this->record)), ',') . ')' . ' VALUES ' . '(' . Formatter::tokenize(implode(',', Formatter::generateUnnamedPlaceholders($this->record)), ',') . ')'; $query = $dbConn->prepare($sql); $query->execute(array_values($this->record)); } } catch (PDOException $e) { return $e->getMessage(); } catch (RecordNotFoundException $e) { return $e->message(); } finally { $dbConn = null; } return $query->rowCount(); }
/** * Test that SQL query is used associatively * */ public function testMakeAssociativeArray() { $this->assertEquals(["token=NULL", 'token_expire="today"'], Formatter::makeAssociativeArray(['' => '', 'token' => null, 'token_expire' => 'today'])); }
/** * @var array classname from class namespace * @return string which is in lower case **/ public static function getClassName($className) { $demarcation = explode('\\', $className); return Formatter::addOrRemoveS(strtolower(end($demarcation))); }