connection = new Mysql(); } /** Carrega os campos de uma tabela */ public function Describe() { /** Consulta SQL */ $this->sql = "describe companies"; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ $this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ); /** Declara o objeto */ $resultDescribe = new \stdClass(); $Field = ''; /** Lista os campos da tabela para objetos */ foreach($this->field as $UsersKey => $Result){ /** Pega o nome do Field/Campo */ $Field = $Result->Field; /** Carrega os objetos como null */ $resultDescribe->$Field = null; } /** Retorna os campos declarados como vazios */ return $resultDescribe; } /** Lista os registros do banco de dados com limitação */ public function Get(int $companyId) { /** Parametros de entrada */ $this->companyId = $companyId; /** Consulta SQL */ $this->sql = 'select cp.*, c.name as city_name from companies cp left join cities c on c.city_id = cp.city_id where cp.company_id = :company_id'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':company_id', $this->companyId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Lista todos os egistros do banco com ou sem paginação*/ public function All(int $start, int $max) { /** Parametros de entrada */ $this->start = $start; $this->max = $max; /** Verifico se há paginação */ if($this->max){ $this->limit = "limit $this->start, $this->max"; } /** Consulta SQL */ $this->sql = 'select * from companies '. $this->limit; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Conta a quantidades de registros */ public function Count() { /** Consulta SQL */ $this->sql = 'select count(company_id) as qtde from companies '; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Insere um novo registro no banco */ public function Save(int $companyId, string $nameBusiness, string $nameFantasy, string $cnpj, string $cep, string $adress, string $number, string $complement, string $district, string $cityId, string $stateId, string $active, int $userId) { /** Parametros */ $this->companyId = $companyId; $this->nameBusiness = $nameBusiness; $this->nameFantasy = $nameFantasy; $this->cnpj = $cnpj; $this->cep = $cep; $this->adress = $adress; $this->number = $number; $this->complement = $complement; $this->district = $district; $this->cityId = $cityId; $this->stateId = $stateId; $this->active = $active; $this->userId = $userId; /** Verifica se o ID do registro foi informado */ if($this->companyId > 0){ /** Consulta SQL */ $this->sql = 'update companies set name_business = :name_business, name_fantasy = :name_fantasy, cnpj = :cnpj, cep = :cep, adress = :adress, number = :number, complement = :complement, district = :district, city_id = :city_id, state_id = :state_id, active = :active where company_id = :company_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId); $this->stmt->bindParam('name_business', $this->nameBusiness); $this->stmt->bindParam('name_fantasy', $this->nameFantasy); $this->stmt->bindParam('cnpj', $this->cnpj); $this->stmt->bindParam('cep', $this->cep); $this->stmt->bindParam('adress', $this->adress); $this->stmt->bindParam('number', $this->number); $this->stmt->bindParam('complement', $this->complement); $this->stmt->bindParam('district', $this->district); $this->stmt->bindParam('city_id', $this->cityId); $this->stmt->bindParam('state_id', $this->stateId); $this->stmt->bindParam('active', $this->active); }else{//Se o ID não foi informado, grava-se um novo registro /** Consulta SQL */ $this->sql = 'insert into companies(company_id, user_id, name_business, name_fantasy, document, cep, adress, number, complement, district, city_id, state_id, active ) values (:company_id, :user_id, :name_business, :name_fantasy, :document, :cep, :adress, :number, :complement, :district, :city_id, :state_id, :active)'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId); $this->stmt->bindParam('users_id', $this->userId);/** Informa o usuário responsável pelo nova empresa cadastrada */ $this->stmt->bindParam('name_business', $this->nameBusiness); $this->stmt->bindParam('name_fantasy', $this->nameFantasy); $this->stmt->bindParam('cnpj', $this->cnpj); $this->stmt->bindParam('cep', $this->cep); $this->stmt->bindParam('adress', $this->adress); $this->stmt->bindParam('number', $this->number); $this->stmt->bindParam('complement', $this->complement); $this->stmt->bindParam('district', $this->district); $this->stmt->bindParam('city_id', $this->cityId); $this->stmt->bindParam('state_id', $this->stateId); $this->stmt->bindParam('active', $this->active); } /** Executo o SQL */ return $this->stmt->execute(); } /** Deleta um determinado registro no banco de dados */ function Delete(int $companyId) { /** Parametros de entrada */ $this->companyId = $companyId; /** Consulta SQL */ $this->sql = 'delete from companies where company_id = :company_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId); /** Executo o SQL */ return $this->stmt->execute(); } /** Fecha uma conexão aberta anteriormente com o banco de dados */ function __destruct() { $this->connection = null; } }