How to recover data from model?

Asked

Viewed 96 times

0

I’m using routing with Drouter and always need to take the data of such store by Slug, for this I need to do 1 select on all routes.

But I don’t know how to get the id, name and Slug from over Sistemamodel.php and so on index php. using the variable $dados.

Says that the variable $dados that does not exist and that 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
    ]);
});

Sistemacontroller.php

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

Sistemamodel.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");
        }
    }
}
  • Are you using Poser-autoloader? You really need to use require? I mean if you use Poser-autoloader wouldn’t it be easier to set the namespaces in Composer.json? Set the class DB::conn, there is no way to know if it is a framework or if it was you who created it.

  • @I’m using the Drouter for routing

1 answer

3


That is wrong:

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

The $dados is a PDOStatement, I may be wrong, but he only iterates with a foreach, it does not count the results, for that you must do it:

$dados->execute();

$count = $dados->rowCount();

if($dados > 0){

Of course if I understood your code, now it seems wrong to me too:

    return $dados;

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

It should stay that way:

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 to read the documentation and avoid writing code randomly without knowing what each function does, links:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.