I'm using doctrine to fetch data from my MySQL database. This is all done in Silex. These are the lines of code that enable Doctrine for me.
$config = new \Doctrine\DBAL\Configuration();
$connParams = array(
'driver' => 'pdo_mysql',
'dbname' => 'webshop',
'host' => 'localhost',
'user' => 'root',
'password' => '',
'charset' => 'utf8'
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connParams, $config);
$app['dbcon'] = $conn;
$app->get('/', function () use ($app){
$user = new User($app['dbcon']);
return $app['twig']->render('home.twig', [
'content' => 'Home',
]);
})->bind('home');
And I'm trying to create a user class for login stuff etc. The user class looks like this:
<?php
namespace Models;
class User
{
private $db;
public function __construct($db)
{
$this->db = $db;
$query = $this->db->prepare("SELECT * FROM users");
$query->execute();
$query = $query->fetchAll();
foreach($query as $user){
print_r($user);
}
}
}
Though, the following is the result:
Array ( [id] => 1 [username] => araguera [password] => password [salt] => ksjdfiwe98ru2w98h )
Why does it return as an array and not as an object? Because I want to be able to do something like "$query->username".