How to retrieve data coming from the model?

0

I'm using routing with DRouter and I always need to get the data from such a store by slug , for that I need make 1 select on all routes.

But I do not know how to get the id , name and slug from there SystemModel.php and there in index.php using the variable $dados .

It says that the variable $dados does not exist and that it is not an object.

index.php

$app = new DRouter\App();
$app->render->setViewsFolder(__DIR__.'/App/Views/');
require 'App/Controllers/SistemaController.php';

/* Login */
$app->get('/:slug/admin', function($slug){
    SistemaController::dadosLoja($slug);
    $this->render->load('lojista/loj_login.php', [
        'id_loja' => $dados->id,
        'nome_loja' => $dados->nome,
        'slug_loja' => $dados->slug
    ]);
});

SystemController.php

require dirname(__FILE__) . '/../Models/SistemaModel.php';
class SistemaController
{
    public static function dadosLoja($slug){
        $dados = SistemaModel::slug($slug);
        if($dados != false){
            return $dados;
        }
    }
}

SystemModel.php

class SistemaModel
{
    public static function slug($slug){
        $dados = DB::conn()->prepare("SELECT * FROM 'admin_lojas' WHERE 'slug' = :slug");
        $dados->bindValue(':slug', $slug, PDO::PARAM_STR);
        $dados->execute();
        if(count($dados) == 1){
            return $dados;
        }
    }
}

DB.php

class DB
{
    private static $conn;
    public function __construct(){}

    public static function conn(){
        try {
            if(is_null(self::$conn)){
                self::$conn = new PDO('mysql:host='.HOST.';dbname='.BD.'',''.USER.'',''.PASS.'',
                array(
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
                ));
            }
            return (self::$conn) ? self::$conn : false;
        } catch(PDOException $e) {
            die("Error");
        }
    }
}
    
asked by anonymous 14.01.2017 / 15:24

1 answer

3

This is wrong:

if(count($dados) == 1){
    return $dados;
}

$dados is PDOStatement , I may be wrong, but it only iterates with a foreach , it does not count the results, so you should do this:

$dados->execute();

$count = $dados->rowCount();

if($dados > 0){

Of course if I understood your code, now that also seems wrong to me:

    return $dados;

You are returning the object, not the data, for this you need $dados->fetchAll(); (returns multiple lines)

It should look like this:

class SistemaModel
{
    public static function slug($slug){
        $dados = DB::conn()->prepare("SELECT * FROM 'admin_lojas' WHERE 'slug' = :slug");
        $dados->bindValue(':slug', $slug, PDO::PARAM_STR);
        $dados->execute();

        if($dados->rowCount() > 0){
            return $dados->fetchAll();
        }
    }
}

If slug returns only one item, then I believe the correct one would be $dados->fetch(PDO::FETCH_ASSOC) :

class SistemaModel
{
    public static function slug($slug){
        $dados = DB::conn()->prepare("SELECT * FROM 'admin_lojas' WHERE 'slug' = :slug");
        $dados->bindValue(':slug', $slug, PDO::PARAM_STR);
        $dados->execute();

        if($dados->rowCount() > 0){
            return $dados->fetch(PDO::FETCH_ASSOC);
        }
    }
}

Documentation

It is highly recommended that you read the documentation and avoid writing random codes without knowing what each function does, links:

14.01.2017 / 16:48