Problems searching php pdo

0

I'm doing a search field but it returns me some errors. My code:

class Prog{
  private $DtBase;

   public function setdata($DtBase){
    $this->data = $DtBase;
   }
   public function getdata(){
    return convertarData($this->data);
   }  

   public function convertarData($data)
   {

    $exp = explode("/",$data);
    return $exp[2].'-'.$exp[1].'-'.$exp[0].'-';
   }
}

progDAO

class ProgDAO{

private $conn;

public function __construct($connection){
    $this->conn = $connection;
}

public function Busca($obj){
        $results = array();
        $stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DtBase = "'.$obj->getdata().'" ');
        $stmt->execute();
        if($stmt) {
            while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                $prog = new Prog();
                $prog->setid($row->ID);
                $prog->setst($row->DsStatus);
                $prog->setcarreta($row->CdCarreta);
                $prog->setplaca($row->NrPlaca);
                $prog->setmot(stripslashes($row->DsMotorista));
                $prog->setsaida(date('d/m/Y', strtotime($row->DtSaida)));
                $prog->setorig($row->DsOrigem);
                $prog->setdest($row->DsDestino);
                $prog->setprev(date('d/m/Y', strtotime($row->DtPrevChegDest)));
                $prog->setcarga($row->DsCarga);
                $prog->setadfin($row->DsAdFin);
                $prog->setagen($row->DsAgendas);
                $prog->setmal($row->DsMalote);
                $prog->setobs($row->DsObservacao);
                $results[] = $prog;
            }
        }
    return $results;
}

progControle

class Comando{

private $conn;

   public function __construct($connec) {
    $this->conn = $connec;
   }

   public function Busca(Comando $obj){
    $dao = new ProgDAO($this->conn);
    return $dao -> Busca($obj);
   }
}

progPrecontrole

include_once ('../connection_open.php');

include_once ('../model/prog.php');
include_once ('progControle.php');
include_once ('../DAO/progDAO.php');

$dataPost = $_POST['data'];

$objProg = new Prog();
$objProg->setdata($dataPost);

$objComando = new Comando();
$objComando->Busca($objProg);


header ("location: ../view/busca.php");

include_once ('../connection_close.php');

<html>
<head>
    <title>Busca</title>
</head>
<body>
    <h1 align="center">Campo para busca</h1>
    <form action="../controller/progPrecontrole.php" method="POST" onsubmit="return valid();">
        Data: <input type="date" id="data" name="data">
        <input type="submit" class="success round button" value="Buscar"/>
    </form>
</body>
</html>

Problems:

  

Warning : Missing argument 1 for Command :: __ construct (), called in C: \ xampp \ htdocs \ Teste2 \ controller \ progPrecontrole.php on line 16 and defined in C: \ xampp \ htdocs \ Teste2 \ controller \ progControle.php on line 7

     

Notice : Undefined variable: connec in C: \ xampp \ htdocs \ Teste2 \ controller \ progControle.php on line 8

     

Catchable fatal error : Argument 1 passed to Command :: Search () must be an instance of Command, instance of Prog given, called in C: \ xampp \ htdocs \ Teste2 \ controller \ progPrecontrole .php on line 17 and defined in C: \ xampp \ htdocs \ Teste2 \ controller \ progControle.php on line 11

    
asked by anonymous 22.03.2016 / 13:39

1 answer

1
  

Missing argument 1 for Command :: __ construct ()

You failed to pass the argument (variable or value) in the constructor of Comando , probably here:

$objComando = new Comando(); //<---- cadê o argumento que é a conexão com o banco?
$objComando->Busca($objProg);
  

Argument 1 passed to Command :: Search () must be an instance of Command, instance of Prog given

The type that was erased for Busca() must be a Comando and not a Prog according to the error message but I think it is intended to pass Prog even since this object has method setData() for this you just need to fix the type in the method signature.

Change:

class Comando{
   //código omitido
   public function Busca(Comando $obj){

To:

class Comando{
   //código omitido
   public function Busca(Prog $obj){

Tips not related to the main problem

  • Use prepared statements in the correct way.

Change:

$stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DtBase = "'.$obj->getdata().'" ');

To:

$stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DtBase = ?');
$stmt->bindValue(1, $obj->getdata());
  • Convert the date in a simplified and correct way (has - more in this method)

Change:

public function convertarData($data){
   $exp = explode("/",$data);
   return $exp[2].'-'.$exp[1].'-'.$exp[0].'-';
}

To:

 public function convertarData($data){
    $data = DateTime::createFromFormat('d/m/Y', $data);
    return $data->format('Y-m-d');
 }
    
22.03.2016 / 13:59