1

I started some days ago with FlightPHP. Now I created my own model called imagePreviewModel.

Here is the model:

<?php
class imagePreviewModel{
    private $db;

    public function __construct(PDO $db){
        $this->db = $db;
    }

    public function getAllImages(){
        return $this->db->query('SELECT * FROM images');
    }
}
?>

Inside of the controller I registered the class and called the method getAllImages(). Now I open the page with the Browser and get an error: Call to undefined method PDO::getAllImages() (0)

Here is the code of the controller:

<?php
require 'flight/Flight.php';
include 'private/models/imagePreviewModel.php';

Flight::route('/', function(){
    Flight::register('imagePreviewModel', 'PDO', array('mysql:host=localhost;dnbname=share','root',''));
    $imagePreviewModel = Flight::imagePreviewModel();
    $List = $imagePreviewModel->getAllImages();


    Flight::render('general', NULL);
});

Flight::start();
?>

Can anyone help me?


I think I found the problem. I register the class PDO, but I want to register a class called 'imagePreviewModel'. How can I register that class? At the FlightPHP page is this example:

// Register your class
Flight::register('user', 'User');

// Get an instance of your class
$user = Flight::user();

But what is the class name and the parameter? And why the wrote two times User? I need a little explanation.

MyNewName
  • 1,035
  • 2
  • 18
  • 34

1 Answers1

0

I see your issue, you're right that you're are trying to register the class PDO and not the class imagePreviewModel, below is how you should declare it.

Flight::register('imagePreviewModel', 'imagePreviewModel');
$list = Flight::imagePreviewModel()->getAllImages();

Please note that the first argument of "register" is the name you want to call, it can be anything you want and the second param is the actual class name.

Just so you know classes should start with a capital letter and match the file name.

I hope this helps