Wallogit.com
2017 © Pedro Peláez
Yesql Bundle for Symfony
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*1)
$ composer require olimsaidov/yesql-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*2)
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:, (*3)
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Ox\YesqlBundle\YesqlBundle(),
);
// ...
}
// ...
}
Then, configure the bundle by adding the following lines
in the app/config/config.yml file of your project:, (*4)
yesql:
connection: default # optional, doctrine custom connection name
services:
-
path: "%kernel.root_dir%/../src/Acme/BlogBundle/Resources/blog.sql" # path to sql file
name: "blog" # service name
Each query in your SQL file must be commented like this:, (*5)
-- name: getAllPosts* -- This will fetch all rows from posts select * from posts; -- name: getPostById -- select * from posts where id = ?; -- name: insertPost -- You can use parametrized placeholder insert into post (title, body) values (:title, :body);
Query name must end with * symbol to query multiple rows., (*6)
Execute your queries by calling the service:, (*7)
$this->get('yesql.blog')->getAllPosts(); // returns all posts as array
$this->get('yesql.blog')->getPostById(3); // returns single post
$this->get('yesql.blog')->insertPost([':title' => 'Hello', ':body' => 'World']); // returns last insert id