sensorario
sensorario

Reputation: 21600

Save records with Silex and Doctrine does not works?

I am trying to create a RESTful architecture with Silex. The first stuff I want to implement is save a meal. I've route "/meals" in post. For debug purpose, I am showing you get request instead of post. This code does not works as I expected. Removing executeUpdate instruction, all works fine. Adding executeUpdate instruction, web page returns a blank page.

<?php

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

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$app = new Silex\Application();

$app['debug'] = true;

$app->register(
    new Silex\Provider\DoctrineServiceProvider(),
    array(
        'db' => array(
            'driver' => 'pdo_mysql',
            'host' => 'localhost',
            'dbname' => 'database',
            'user' => 'user',
            'password' => 'password',
            'charset' => 'utf8',
        ),
    )
);

$app->get(
    '/meals',
    function (Request $request) use ($app) {
        $app['db']->executeUpdate(
            'INSERT INTO meal_items (id, id_meal, food) VALUES (?, ?, ?)',
            array(
                null,
                1,
                'Onion'
            )
        );

        return new Response(json_encode(
            array(
                'hello'
            )
        ), 201);
    }
);

$app->run();

Upvotes: 0

Views: 962

Answers (1)

ivoba
ivoba

Reputation: 5986

You have to name it db.options instead of db in your array that you pass to the provider.

See here:

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array(
        'driver' => 'pdo_mysql',
        'host' => 'localhost',
        'dbname' => 'database',
        'user' => 'user',
        'password' => 'password',
        'charset' => 'utf8',
    ),
));

Upvotes: 1

Related Questions