/** * Saves entity to database * * @param $entity - object * * @return boolean */ public function save($entity) { $entityName = $this->dbHelper->getEntityName($entity); $tableName = $this->dbHelper->getUnderscoreName($entityName); $data = $this->dbHelper->getInsertingData($entity); $query = "INSERT INTO `{$tableName}` (%s) VALUES (%s)"; $values = []; $fields = ''; $subs = ''; foreach ($data as $field => $value) { $subs .= "?, "; $fields .= "`{$field}`, "; $values[] = $value; } $subs = substr($subs, 0, strlen($subs) - 2); $fields = substr($fields, 0, strlen($fields) - 2); try { $stmt = $this->pdo->prepare(sprintf($query, $fields, $subs)); for ($i = 0; $i < count($values); $i++) { $type = $this->dbHelper->getValueType($values[$i]); $stmt->bindParam($i + 1, $values[$i], $type); } $stmt->execute(); } catch (\Exception $e) { die($e->getMessage()); } return true; }
public function testGetValueType() { $this->assertEquals(\PDO::PARAM_INT, $this->helper->getValueType(12)); $this->assertEquals(\PDO::PARAM_STR, $this->helper->getValueType('12')); $this->assertEquals(\PDO::PARAM_BOOL, $this->helper->getValueType(true)); $this->assertEquals(\PDO::PARAM_NULL, $this->helper->getValueType(null)); $this->assertEquals(\PDO::PARAM_STR, $this->helper->getValueType(12.12)); }