I want to add entities together with Silex. So that I can move the queries out of the controller. I have created an entity class, but when I call like:
$sql = "SELECT * FROM todos WHERE id = '$id'";
$product = $app['db']->fetchAssoc($sql);
I got an error that $app is not defined.
Entity/Products.php
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
use Silex\Application;
use Silex\ServiceProviderInterface;
/**
* @ORM\Entity
* @ORM\Table(name="Product")
*/
class Product implements ServiceProviderInterface
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
*/
private $id;
private $userId;
private $description;
public function showProduct($user, $id) {
if ($id){
$sql = "SELECT * FROM product WHERE id = '$id'";
$product = $app['db']->fetchAssoc($sql);
return $app['twig']->render(‚product.html', [
‚product' => $product,
]);
} else {
$sql = "SELECT * FROM product sWHERE user_id = '${user['id']}'";
$products = $app['db']->fetchAll($sql);
return $app['twig']->render('todos.html', [
‚products‘ => $products,
]);
}
}
}
I am having a Controller which is working fine. Also I am having an app-php file where I have registered the entity class:
app.php
<?php
use Silex\Application;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Silex\Provider\DoctrineServiceProvider;
use DerAlex\Silex\YamlConfigServiceProvider;
use Entity\Product;
$app = new Application();
$app->register(new SessionServiceProvider());
$app->register(new UrlGeneratorServiceProvider());
$app->register(new ValidatorServiceProvider());
$app->register(new ServiceControllerServiceProvider());
$app->register(new TwigServiceProvider());
$app->register(new HttpFragmentServiceProvider());
$app->register(new Product());
$app->register(new YamlConfigServiceProvider(__DIR__.'/../config/config.yml'));
$app->register(new DoctrineServiceProvider, array(
'db.options' => array(
'driver' => 'pdo_mysql',
'host' => $app['config']['database']['host'],
'dbname' => $app['config']['database']['dbname'],
'user' => $app['config']['database']['user'],
'password' => $app['config']['database']['password'],
'charset' => 'utf8',
),
));
return $app;
controller.php
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$twig->addGlobal('user', $app['session']->get('user'));
return $twig;
}));
$app->get('/', function () use ($app) {
return $app['twig']->render('index.html', [
'readme' => file_get_contents('README.md'),
]);
});
$app->match('/login', function (Request $request) use ($app) {
$username = $request->get('username');
$password = $request->get('password');
if ($username) {
$sql = "SELECT * FROM users WHERE username = '$username' and password = '$password'";
$user = $app['db']->fetchAssoc($sql);
if ($user){
$app['session']->set('user', $user);
return $app->redirect('/todo');
}
}
return $app['twig']->render('login.html', array());
});
$app->get('/logout', function () use ($app) {
$app['session']->set('user', null);
return $app->redirect('/');
});
$app->get('/product/{id}', function ($id) use ($app) {
if (null === $user = $app['session']->get('user')) {
return $app->redirect('/login');
}
if ($id){
$sql = "SELECT * FROM product WHERE id = '$id'";
$product = $app['db']->fetchAssoc($sql);
return $app['twig']->render('product.html', [
'product' => $product,
]);
} else {
$sql = "SELECT * FROM product WHERE user_id = '${user['id']}'";
$productss = $app['db']->fetchAll($sql);
return $app['twig']->render('products.html', [
'products' => $products,
]);
}
})
->value('id', null);
How can I use $app in the Entity? Or do I have to change something in general?
Before I have just used Symfony and not Silex.