$debitosAnteriores=array_column(tbdiario::groupBy('contad')
->select(['contad','sum(valor) as debito_anterior'],
[['dia','<',$primeiroDia]]),'debito_anterior','contad');
eu precisei alterar a classe bd:
listagem parcial de config.php
<?php
class bd {privatestatic $pdo;privatestatic $initialized =false;privatestatic $orderBy ='';privatestatic $groupBy ='';// Adicionando a variável para a cláusula GROUP BYstaticfunction initialize(){if(!self::$initialized){
$dsn ='mysql:host='. HOST .';dbname='. DBNAME;self::$pdo =new PDO($dsn, USER, PASSWORD);self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);self::$initialized =true;}}protectedstaticfunction getPdo(){self::initialize();returnself::$pdo;}protectedstaticfunction getTableName(){return'';// Será implementado na classe filha}publicstaticfunction orderBy($column, $direction ='ASC'){self::$orderBy ="ORDER BY $column $direction";returnnewstatic;}// Adicionando o método groupBypublicstaticfunction groupBy($columns){if(is_array($columns)){
$columns = implode(', ', $columns);}self::$groupBy ="GROUP BY $columns";returnnewstatic;}publicstaticfunctionselect($columns ='*', $conditions =[]){self::initialize();
$table =static::getTableName();if(is_array($columns)){
$columns = implode(', ', $columns);}
$whereClause ='';if(!empty($conditions)){foreach($conditions as $condition){
$field = $condition[0];
$operator = $condition[1];
$value = $condition[2];
$whereClause .="$field $operator :$field AND ";}
$whereClause ='WHERE '. rtrim($whereClause,' AND ');}
$sql ="SELECT $columns FROM $table $whereClause";if(self::$groupBy){
$sql .=' '.self::$groupBy;self::$groupBy ='';// Reset groupBy after use}if(self::$orderBy){
$sql .=' '.self::$orderBy;self::$orderBy ='';// Reset orderBy after use}
$stmt =self::getPdo()->prepare($sql);if(!empty($conditions)){foreach($conditions as $condition){
$field = $condition[0];
$value = $condition[2];
$stmt->bindValue(":$field", $value);}}
$stmt->execute();return $stmt->fetchAll(PDO::FETCH_OBJ);}}class tbdiario extends bd {protectedstaticfunction getTableName(){return'tbdiario';}}class tbconta extends bd {protectedstaticfunction getTableName(){return'tbconta';}}class tbusuarios extends bd {protectedstaticfunction getTableName(){return'tbusuarios';}}
Pergunta
Frank K Hosaka
Para executar esse comando:
$debitosAnteriores=array_column(tbdiario::groupBy('contad') ->select(['contad','sum(valor) as debito_anterior'], [['dia','<',$primeiroDia]]),'debito_anterior','contad');
eu precisei alterar a classe bd:
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.