Silex与ORM接入层和Entity,如何?

Silex with ORM access layer and Entity, how to?

提问人:Gunnar Polte 提问时间:12/27/2019 最后编辑:Gunnar Polte 更新时间:12/28/2019 访问量:109

问:

我想将实体与 Silex 一起添加。这样我就可以将查询移出控制器。 我已经创建了一个实体类,但是当我调用时:

 $sql = "SELECT * FROM todos WHERE id = '$id'";
            $product = $app['db']->fetchAssoc($sql);

我收到一个错误,$app未定义。

实体/产品 .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,
            ]);
        }
    }


}

我有一个工作正常的控制器。 另外,我有一个app-php文件,我在其中注册了实体类:

应用程序.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;

控制器 .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);

如何在实体中使用$app? 还是我必须改变一些东西?

之前我只用了 Symfony 而不是 Silex。

php symfony silex

评论

0赞 Jeto 12/27/2019
您需要将$app注入到服务控制器中。您到底在哪里运行该 SQL 查询?注意:1)该查询看起来容易发生SQL注入,2)Silex不再维护。
0赞 Gunnar Polte 12/27/2019
谢谢你的回答。当前,该查询正在控制器中运行。我想在它的实体或存储库中运行它。是的,它必须以某种方式注射。我真的不知道该怎么做。我以为是通过在应用程序文件中注册实体.php文件:$app->register(new Product());
0赞 Jeto 12/27/2019
请使用所有相关代码编辑您的问题(您如何声明您的控制器,您引用的位置等)$app
0赞 Gunnar Polte 12/27/2019
我已经添加并调整了代码。
0赞 Jeto 12/28/2019
public function show Product($user, $id) {<- 这不是有效的 PHP。此外,这不是您应该使用服务提供商的方式。查看此部分了解详细信息和完整示例。我相信你混淆了 Entites(一个 Doctrine 的东西)和服务提供商(一个 Silex 的东西)。

答: 暂无答案