0

couldn't find an answer for my question so here it goes.

I have a config.php file with the information for database connection:

<?php

declare(strict_types=1);

namespace App\Model;

// COMPOSER AUTOLOAD
require '../vendor/autoload.php';

// ROUTER FOR CONFIG PAGE
class Config
{  
// LOCAL DATABASE
 public $Database_Name = "power";
 public $Database_User = "root";
 public $Database_Password = "";   
}

I have a Database.php file with the PDO connection:

<?php

declare(strict_types=1);

namespace App\Model;

use PDO;
use Exception;

include "../public/Config.php";

// CLASSE DATABASE
class Database
{
    private static $bdd = null;
    // MAIN DATABASE CONNECTION USED BY ALL METHODS

    public static function getBdd()
        {
        if (self::$bdd == null) {
            $config = new Config;
            try {
                self::$bdd = new PDO('mysql:host=localhost; dbname=' . $config->Database_Name, $config- >Database_User, $config->Database_Password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            } catch (Exception $e) {
                echo "Cannot reach Database";
                die;
            }            
        }
        return self::$bdd;
    }

}

I have my Class with getters and setters that i want to instanciate and fill with the data from the database:

<?php

declare(strict_types=1);

namespace App\Model;

class Group
{

private $id;
private $group_admin;
private $creation_date;
private $group_name;
private $group_description;
private $group_status;

// CONSTRUCT
public function __construct($id, $group_admin, $creation_date, $title, $description, $status)
{
    $this->id = $id;
    $this->group_admin = $group_admin;
    $this->creation_date = $creation_date;
    $this->group_name = $title;
    $this->group_description = $description;
    $this->group_status = $status;
}

/**
 * Get the value of id
 */
public function getId()
{
    return $this->id;
}

/**
 * Set the value of id
 *
 * @return  self
 */
public function setId($id)
{
    $this->id = $id;

    return $this;
}

..........

And finally i have my Models with the code i want to execute to get the data and fill my classes:

<?php

namespace App\Model;

use PDO;
use App\Model\Group; // THIS SHOULD IMPORT THE GROUP CLASS

class GroupModel
{

    public function getMyGroups(): array
    {
        $bdd = Database::getBdd();
        $req = $bdd->prepare("SELECT * FROM groups ORDER BY creation_date DESC");
        $req->execute();
        // DEBUG
        // $req->debugDumpParams();
        // die;
        $Group = new Group(null, null, null, null, null, null);
        $result = $req->fetchall(PDO::FETCH_CLASS, 'Group');
        var_dump($result);
        die;
        return $result;
    }

... problem is, the class is unseen by my code.

What am i missing here?

I currently get the error:

Fatal error: Class 'Group' not found in D:\Dropbox\...\...\OCR-P5\src\Model\GroupModel.php on line 43

I also tryed with Group::class or by instantiating the class but no, like this:

$result = $req->fetchall(PDO::FETCH_CLASS, Group::class );

It doesn't work either and cannot find the class.

I also noticed that i am using the PDO expression in two places (1 - in the file that establishes the connection, 2 - in the last Model method fetchall(), not sure if i should.

Best regards.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Rui Ruivo
  • 343
  • 3
  • 12
  • 1
    Try using `$req->fetchall(PDO::FETCH_CLASS, 'App\Model\Group');` – Barmar Jan 23 '20 at 10:06
  • OK, got this: Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: could not call class constructor in D:\Dropbox\OPENCLASSROOMS\PROJET_P5\OCR-P5\src\Model\GroupModel.php on line 20 Fatal error: Uncaught ArgumentCountError: Too few arguments to function App\Model\Group::__construct(), 0 passed and exactly 6 expected ... – Rui Ruivo Jan 23 '20 at 10:08
  • 2
    Your problem is irrelevant to PDO. If you'd try to just `new Group` the result will be the same – Your Common Sense Jan 23 '20 at 10:11
  • with this: $result = $req->fetchall(PDO::FETCH_CLASS, 'App\Model'.'\\Group'); I get this: Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: could not call class constructor in D:\Dropbox\OPENCLASSROOMS\PROJET_P5\OCR-P5\src\Model\GroupModel.php on line 20 Fatal error: Uncaught ArgumentCountError: Too few arguments to function App\Model\Group::__construct(), 0 passed and exactly 6 expected – Rui Ruivo Jan 23 '20 at 10:12
  • How do i send arguments? – Rui Ruivo Jan 23 '20 at 10:13
  • 2
    `FETCH_CLASS` doesn't call the constructor with the row data. It calls the constructor with no arguments, then assigns to each property itself. So you need to make the constructor arguments optional. – Barmar Jan 23 '20 at 10:16
  • Found my solution here: https://stackoverflow.com/questions/28395855/how-to-create-constructor-with-optional-parameters Thank You – Rui Ruivo Jan 23 '20 at 10:28

0 Answers0