使用select照亮\数据库查询问题

时间:2018-03-31 14:17:38

标签: php eloquent slim

我试图测试各种查询结构和大多数工作。

<?php
namespace App\Controllers;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class HomeController extends Controller
{
    public function index($request, $response)
    {
        //return var_dump($container);
        //$user=$this->db->table('users')->find(1);
        //$user=$this->db->table('users')->where('id',1)->get();
        //$user=$this->db->table('users')->where('name','alex')->get();
        $user=$this->db->select('select * from users where id = ?', array(1));
        var_dump($user);
        die();
        return $this->view->render($response, 'home.twig');
    }
}

删除的查询正在运行,但var dump之前的最后一次没有。

我得到的错误是:

Call to undefined method Illuminate\Database\Capsule\Manager::select()

这是配置和来电者页面:

<?php
session_start();

require __DIR__ . '/../vendor/autoload.php';

// $user = new \App\models\User;
// var_dump($user);
// die();

$app = new \Slim\app([
    'settings' => [
        'displayErrorDetails' => true,
        'db' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'codecourse',
            'username' => 'root',
            'password' => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
         ]
    ],

]);

// Get container
$container = $app->getContainer();

//set illuminate capsule
$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

$container['db'] = function ($container) use ($capsule){
    return $capsule;
};


//Register Twig View helper
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
        'cache' => false/**'path/to/cache'**/,
    ]);

    // Instantiate and add Slim specific extension
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container->router,
        $container->request->getUri()
    ));

    return $view;
};


$container['HomeController'] = function($container){
    return new App\Controllers\HomeController($container);
};

免责声明这是基于我所遵循的教程, Slim是我学习的第一个框架,我不知道如何调试这个问题。

2 个答案:

答案 0 :(得分:1)

Use DB; //Top of your page

$user= DB::Select('users')->where('id','=',[$yourIds]);

答案 1 :(得分:1)

添加并自定义此容器条目:

use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Connection;
use Psr\Container\ContainerInterface as Container;

$container['db'] = function (Container $container) {
    $settings = $container->get('settings');
    $config = [
        'driver' => 'mysql',
        'host' => $settings['db']['host'],
        'database' => $settings['db']['database'],
        'username' => $settings['db']['username'],
        'password' => $settings['db']['password'],
        'charset'  => $settings['db']['charset'],
        'collation' => $settings['db']['collation'],
        'prefix' => '',
    ];

    $factory = new ConnectionFactory(new \Illuminate\Container\Container());

    return $factory->make($config);
};

然后生成并执行查询。使用get()方法获取所有行:

$query = $this->db->table('users');
$query->select('id', 'username', 'email');
$query->where('id', '=', 1);

// fetch all rows (as stdClass)
$rows = $query->get();

// Alternative: fetch all rows as array
$rows = $query->get()->toArray();

var_dump($rows);

使用PDO的原始查询:

// Get the PDO object
$pdo = $this->db->getPdo();

// Use prepared statements
$statement = $pdo->prepare("SELECT * FROM users WHERE id= :id");
$statement->execute(['id' => 1]);
$userRow = $statement->fetch();