olimsaidov / yesql-bundle
为Symfony的Yesql Bundle
dev-master
2018-01-16 11:01 UTC
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2024-08-29 04:25:55 UTC
README
步骤 1:下载Bundle
打开命令行界面,进入您的项目目录,并执行以下命令以下载此Bundle的最新稳定版本
$ composer require olimsaidov/yesql-bundle
此命令要求您全局安装Composer,请参考Composer文档中的安装章节。
步骤 2:启用Bundle
然后,通过将Bundle添加到项目app/AppKernel.php
文件中注册的Bundle列表中来启用它
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Ox\YesqlBundle\YesqlBundle(), ); // ... } // ... }
步骤 3:配置Bundle
然后,通过在项目app/config/config.yml
文件中添加以下行来配置Bundle
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
您的SQL文件中的每个查询必须按以下方式注释
-- 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);
查询名称必须以*
符号结尾才能查询多行。
步骤 4:使用Bundle
通过调用服务来执行您的查询
$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