почему класс не найден в php pdo - PullRequest
0 голосов
/ 24 февраля 2020

привет, я пытаюсь вернуть массив int с этой функцией

, но php не распознает мой класс, помещает эту ошибку "Неустранимая ошибка: класс 'obj \ EntitePiece' не найден в [my путь] \ web \ InfoWeb-TP-CRUD \ L3_TP_InfoWeb_ CSS \ php \ class \ MyPDO. php в строке 273 "

, 2 класса находятся в одной папке

public function getAllValeurPiece() : array{
        $result = array();
        $tab = $this->query("SELECT distinct * FROM piece order by valeur")
            ->fetchAll(PDO::FETCH_CLASS, 'obj\EntitePiece');

        foreach ($tab as $value){
            array_push($result, $value->getValeur());
        }

        print $result[0];

        return $result;
    }

и мой класс EntitePiece

<?php
namespace obj;

class EntitePiece{

  /**
  * identifiant de la piece
  * @var int
  */
  protected $id_piece;

  /**
  * face de la piece
  * @var int
  */
  protected $face;

  /**
  * valeur faciale de la piece
  * @var float
  */
  protected $valeur;

  /**
  * diametre de la piece
  * @var float
  */
  protected $diametre;

  /**
  * epaisseur de la piece
  * @var float
  */
  protected $epaisseur;

  /**
  * masse de la Piece
  * @var float
  */
  protected $masse;

  /**
  * materiau de la piece
  * @var int
  */
  protected $materiau;

  /**
  * @return $this->id_piece
  */
  public function getId_piece() : int {
    return $this->id_piece;
  }

  /**
  * @param $id_piece
  */
  public function setId_piece($id_piece) : void {
    $this->id_piece=$id_piece;
  }

  /**
  * @return $this->$face
  */
  public function getFace() : int {
    return $this->face;
  }

  /**
  * @param $face
  */
  public function setFace($face) : void {
    $this->face=$face;
  }

  /**
  * @return $this->diametre
  */
  public function getDiametre() : float {
    return $this->diametre;
  }

  /**
  * @param $diametre
  */
  public function setDiametre($diametre) : void {
    $this->diametre=$diametre;
  }

  /**
  * @return $this->epaisseur
  */
  public function getEpaisseur() : float {
    return $this->epaisseur;
  }

  /**
  * @param $epaisseur
  */
  public function setEpaisseur($epaisseur) : void {
    $this->epaisseur=$epaisseur;
  }

  /**
  * @return $this->masse
  */
  public function getMasse() : float {
    return $this->masse;
  }

  /**
  * @param $masse
  */
  public function setMasse($masse) : void {
    $this->masse=$masse;
  }

  /**
  * @return $this->materiau
  */
  public function getMateriau() : int {
    return $this->materiau;
  }

  /**
  * @param $materiau
  */
  public function setMateriau($materiau) : void {
    $this->materiau=$materiau;
  }

    /**
     * @return float
     */
    public function getValeur(): float
    {
        return $this->valeur;
    }

    /**
     * @param float $valeur
     */
    public function setValeur(float $valeur): void
    {
        $this->valeur = $valeur;
    }



  public static function getTestEntitePiece(): EntitePiece
  {
      $test = new EntitePiece();
      $test->setDiametre(1);
      $test->setEpaisseur(1);
      $test->setValeur(1);
      $test->setFace(1);
      $test->setMasse(1);
      $test->setId_piece(1);
      $test->setMateriau(1);
      return $test;
  }
}

всего класса pdo, не обращайте внимания на определенные методы, которые исправляются

<?php
namespace obj;


use PDO;
use PDOStatement;

use obj\EntiteFace;
use obj\EntitePays;
use obj\EntitePiece;
use obj\EntiteMateriau;
use obj\EntiteQuantite;

class MyPDO extends PDO
{
    /**
     * gestion statique des accès SGBD
     * @var PDO
     */
    private $_pdo;

    /**
     * @var PDOStatement
     */
    private $_pdos_selectAll;

    /**
     * gestion statique de la requête préparée de selection
     * @var PDOStatement
     */
    private $_pdos_select;

    /**
     * gestion statique de la requête préparée de mise à jour
     * @var PDOStatement
     */
    private $_pdos_update;

    /**
     * gestion statique de la requête préparée d'insersion
     * @var PDOStatement
     */
    private $_pdos_insert;

    /**
     * gestion statique de la requête préparée de supression
     * @var PDOStatement
     */
    private $_pdos_delete;

    /**
     * PreparedStatement associé à un SELECT, calcule le nombre de pièces dans la table
     * @var PDOStatement;
     */
    private $_pdos_count;

    /**
     * @var string
     */
    private $nomTable;

    /**
     * MyPDO constructor.
     * @param $sgbd
     * @param $host
     * @param $db
     * @param $user
     * @param $password
     * @param $nomTable
     */

    public function __construct($sgbd, $host, $db, $user, $password, $nomTable)
    {
        parent::__construct("$sgbd:host=".$host.";dbname=".$db, $user, $password);

        // pour récupérer aussi les exceptions provenant de PDOStatement
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->nomTable = $nomTable;

    }


    public function initPDOS_selectAll() {
        $this->_pdos_selectAll = $this->_pdo->prepare('SELECT * FROM'.$this->nomTable);
    }

    /**
     * préparation de la requête SELECT * FROM $this->nomTable WHERE $nomColId = :id
     * instantiation de $this->pdos_select
     * @param string $nomColID
     */
    public function initPDOS_select(string $nomColID = "id"): void
    {
        $requete = "SELECT * FROM ".$this->nomTable ." WHERE $nomColID = :$nomColID";
        $this->_pdos_select = $this->_pdo->prepare($requete);
    }

    /**
     * @param string $nomColId
     * @param array $colNames
     */
    public function initPDOS_update(string $nomColId, array $colNames): void {
        $query = "UPDATE ".$this->nomTable." SET ";
        foreach ($colNames as $colName) {
            $query .= $colName."=:".$colName.", ";
        }
        $query = substr($query,0, strlen($query)-2);
        $query .= " WHERE ".$nomColId."=:".$nomColId;
        $this->_pdos_update =  $this->_pdo->prepare($query);
    }

    /**
     * @param string $id
     * @param array $assoc
     */
    public function update(string $id, array $assoc): void {
        if (! isset($this->_pdos_update))
            $this->initPDOS_update($id, array_keys($assoc));
        foreach ($assoc as $key => $value) {
            $this->getPdosUpdate()->bindValue(":".$key, $value);
        }
        $this->getPdosUpdate()->execute();
    }


    /**
     * @param array
     */
    public function initPDOS_insert(array $colNames): void {
        $query = "INSERT INTO ".$this->nomTable." VALUES(";
        foreach ($colNames as $colName) {
            $query .= ":".$colName.", ";
        }
        $query = substr($query,0, strlen($query)-2);
        $query .= ')';
        $this->_pdos_insert = $this->_pdo->prepare($query);
    }

    /**
     * @param array $assoc
     */
    public function insert(array $assoc): void {
        if (! isset($this->_pdos_insert))
            $this->initPDOS_insert(array_keys($assoc));
        foreach ($assoc as $key => $value) {
            $this->getPdosInsert()->bindValue(":".$key, $value);
        }
        $this->getPdosInsert()->execute();
    }

    /**
     * @param string
     */
    public function initPDOS_delete(string $nomColId = "id"): void {
        $this->_pdos_delete = $this->_pdo->prepare("DELETE FROM ". $this->nomTable
            ." WHERE $nomColId=:".$nomColId);
    }

    /**
     * @param array $assoc
     */
    public function delete(array $assoc) {
        if (! isset($this->_pdos_delete))
            $this->initPDOS_delete(array_keys($assoc)[0]);
        foreach ($assoc as $key => $value) {
            $this->getPdosDelete()->bindValue(":".$key, $value);
        }
        $this->getPdosDelete()->execute();
    }

    public function initPDOS_count() {
        $this->_pdos_count = $this->_pdo->prepare('SELECT COUNT(*) FROM '.$this->nomTable);
    }

    /**
     * @return PDO
     */
    public function getPdo(): PDO
    {
        return $this->_pdo;
    }

    /**
     * @return PDOStatement
     */
    public function getPdosSelect() : PDOStatement
    {
        return $this->_pdos_select;
    }


    /**
     * @return PDOStatement
     */
    public function getPdosSelectAll(): PDOStatement
    {
        return $this->_pdos_selectAll;
    }

    /**
     * @return PDOStatement
     */
    public function getPdosUpdate(): PDOStatement
    {
        return $this->_pdos_update;
    }

    /**
     * @return PDOStatement
     */
    public function getPdosInsert(): PDOStatement
    {
        return $this->_pdos_insert;
    }

    /**
     * @return PDOStatement
     */
    public function getPdosDelete(): PDOStatement
    {
        return $this->_pdos_delete;
    }

    /**
     * @return PDOStatement
     */
    public function getPdosCount(): PDOStatement
    {
        return $this->_pdos_count;
    }

    /**
     * @return string
     */
    public function getNomTable(): string
    {
        return $this->nomTable;
    }

    public function constructPiece($id = 0): EntitePiece {
        $piece = $this->query("SELECT * from piece where id_piece = $id")->fetchObject("obj\EntitePiece");;

        return $piece;
    }

    public  function constructFace($id): EntiteFace {
        $face = $this->query("SELECT * from face where id_face = $id")->fetchObject("obj\EntiteFace");
        return $face;
    }

    public function constructPays($id): EntitePays {
        $pays = $this->query("SELECT * from pays where id_pays = $id")->fetchObject("obj\EntitePays");

        return $pays;
    }

    public function constructMateriau($id): EntiteMateriau {
        $materiau = $this->query("SELECT * from materiau where id_materiau = $id")->fetchObject("obj\EntiteMateriau");

        return $materiau;
    }

    // id est ici l'id de la face et non l'id de
    public function constructQuantite($id): EntiteQuantite {
        $quantite = $this->query("SELECT * from quantite where face = $id")->fetchObject("obj\EntiteQuantite");

        $quantite->setQuantite($quantite);
    }

    public function getAllValeurPiece() : array{
        $result = array();
        $tab = $this->query("SELECT distinct * FROM piece order by valeur")
            ->fetchAll(PDO::FETCH_CLASS, 'obj\EntitePiece');

        foreach ($tab as $value){
            array_push($result, $value->getValeur());
        }

        print $result[0];

        return $result;
    }

    public function getPiecePays(int $idpays): array{
        $query = $this->query("SELECT * from piece where face in ( SELECT id_face from face where pays = $idpays);");
        $result = array();

        foreach ($query as $id){
            $piece = $this->constructPiece($id);
            array_push($result, $piece);
        }

        return $result;
    }

    public function getAllPays() : array{
        $result = $this->query("SELECT distinct * FROM pays order by nom_pays")
            ->fetchAll(PDO::FETCH_CLASS, 'obj\EntitePays');
        return $result;
    }

    public function getAllFace() : array{
        $result = $this->query("SELECT distinct * FROM face order by description")
            ->fetchAll(PDO::FETCH_CLASS, 'obj\EntiteFace');
        return $result;
    }

    public function getAllMateriau() : array{
        $result = $this->query("SELECT distinct * FROM materiau order by description")
            ->fetchAll(PDO::FETCH_CLASS, 'obj\EntiteMateriau');
        return $result;
    }

    public function updatePiece(EntitePiece $piece) : void{
        $this->query("UPDATE piece SET
        face = $piece->getFace(),
        valeur = $piece->getValeur(),
        diametre = $piece->getDiametre(),
        epaisseur = $piece->getEpaisseur(),
        masse = $piece->getMasse(),
        materiau = $piece->getMateriau()
        WHERE id_piece = $piece->getId_piece()");
    }

    public function deletePiece(EntitePiece $piece) : void{
        $this->query("DELETE from piece WHERE id_piece = $piece->getId_piece()");
    }

    public function updateFace(EntiteFace $face) : void{
        $this->query("UPDATE face SET
        commune = $face->getCommune(),
        pays = $face->getPays(),
        description = $face->getDescription_face()
        WHERE id_face = $face->getId_face()");
    }

    public function deleteFace(EntiteFace $face) : void{
        $this->query("DELETE from face WHERE id_face = $face->getId_face()");
    }

    public function updateMateriau(EntiteMateriau $mat) : void{
        $this->query("UPDATE materiau SET
        description = $mat->getDescription_materiau()
        WHERE id_materiau = $mat->getId_materiau()");
    }

    public function deleteMateriau(EntiteMateriau $mat) : void{
        $this->query("DELETE FROM materieau WHERE id_materiau = $mat->getId_materiau()");
    }

    public function updatePays(EntitePays $pays) : void{
        $this->query("UPDATE pays SET
        nom_pays = $pays->getNom_pays()
        WHERE id_pays = $pays->getId_pays()");
    }

    public function deletePays(EntitePays $pays) : void{
        $this->query("DELETE FROM pays WHERE id_pays = $pays->getId_pays()");
    }

    public function updateQuantite(EntiteQuantite $qtt) : void{
        $this->query("UPDATE quantite SET
        quantite = $qtt->getQuantite(),
        date = $qtt->getDate()
        WHERE face = $qtt->getQ_face()");
    }

    public function deleteQuantite(EntiteQuantite $qtt) : void{
        $this->query("DELETE FROM quantite WHERE face = $qtt->getQ_face()");
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...