public function addUser($data)
 {
     return $this->userDao->insert($data);
 }
Ejemplo n.º 2
0
    }
    private function query($sql)
    {
        $statement = $this->getDb()->query($sql, PDO::FETCH_ASSOC);
        if ($statement === false) {
            throw new Exception('fail to create the statement');
        }
        return $statement;
    }
    private function execute($sql, User $user)
    {
        $statement = $this->getDb()->prepare($sql);
        $this->executeStatement($statement, $user->toArray());
        if (!$user->getUserId()) {
            return $this->findById($this->getDb()->lastInsertId());
        }
        if (!$statement->rowCount()) {
            throw new Exception('User with ID "' . $user->getUserId() . '" does not exist');
        }
        return $user;
    }
    private function executeStatement(PDOStatement $statement, array $params)
    {
        if (!$statement->execute($params)) {
            throw new Exception('fail to execute the statement');
        }
    }
}
$userDao = new UserDao();
$userDao->insert(new User());