symfony3.x 命令行操作Command

简介: Symfony3.x.x通过命令行操作数据库 配置app/config/parameters.yml parameters: database_host: 127.0.0.1 database_port: 3306 database_name: test data.

Symfony3.x.x通过命令行操作数据库

  1. 配置app/config/parameters.yml

    parameters:
        database_host: 127.0.0.1
        database_port: 3306
        database_name: test
        database_user: root
        database_password: null
        mailer_transport: smtp
        mailer_host: 127.0.0.1
        mailer_user: 127001@qq.com
        mailer_password: null
        secret: 8ab34c9326ac123b2dea2fab13e4ab
  2. 配置app/config/config.yml

    doctrine:
        dbal:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
  3. 创建TestBundle
  4. 在TestBundle目录(文件夹)下,新建Command目录(文件夹)
  5. 在Command目录新建TestCommand.php,代码如下:

    <?php
    //src/TestBundle/Command/TestCommand.php
    namespace TestBundle\Command;
    
    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputOption;
    
    class TestCommand extends ContainerAwareCommand
    {
        protected function configure()
        {
            $this->setName('database:run')
                ->setDescription('Run an action.')
                ->addArgument('name', InputArgument::OPTIONAL, 'Chouse an action to run.')
                ->addOption('show', null, InputOption::VALUE_NONE, 'Show result.');
        }
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $name = $input->getArgument('name');
            if ($name == 'action1'){
                $text = $this->myAction1();
            }elseif($name == 'action2')
            {
                $text = $this->myAction2();
            }elseif($name == 'action3')
            {
                $text = $this->myAction3();
            }else
            {
                $text = 'Error[101]:Cmd error.';
            }
            if (!$input->getOption('show')){
                $text = NULL;
            }
            $output->writeln($text);
        }
        protected function myAction1()
    {
    //这句非常重要
            $conn = $this->getContainer()->get('database_connection');
            $bool= $conn->exec(
    //这里的SQL语句自己定义
                'INSERT INTO 表名(id,live,age)VALUE(1,\'dog\',20)'
            );
            $conn = null;
            if($bool)
                $str = "Action1 has been executed.";
            else
                $str = "Not insert data.";
            return $str;
        }
        protected function myAction2()
        {
            if(true)
                $str = "Action2 has been executed.";
            else
                $str = "Did not perform any action!";
            return $str;
        }
        protected function myAction3()
        {
            if(true)
                $str = "Action3 has been executed.";
            else
                $str = "Did not perform any action!";
            return $str;
        }
    }
  6. 尝试在命令行执行:

    Php bin/console --show database:run action1

    (1)如果返回:Action1 has been executed.说明操作成功;

    (2)如果返回:Not insert data.就要检查自己的sql语句是否有误

  7. 如果命令行中不输入:‘--show’表示不显示执行结果。
  8. action1 也可以是action2、action3。

意义

Symfony通过命令行操作数据库的意义在于

  1. 可以将方法不和URL进行绑定,实现方法的运行,有效防止sql注入;
  2. 由于命令定义的灵活性,非内部人员不知道你的命令行的格式,也不知道其实现何种操作。
目录
相关文章
|
4月前
|
网络协议 Oracle 关系型数据库
HCL与Pipe、Autoit和MobaXterm的组合使用
HCL与Pipe、Autoit和MobaXterm的组合使用
52 0
|
6月前
|
Docker Windows 容器
本地启动 ABAP Platform Trial 的 Docker 命令行程序
本地启动 ABAP Platform Trial 的 Docker 命令行程序
50 0
|
8月前
小匕首-dotnet cli使用tool指令
小匕首-dotnet cli使用tool指令
70 0
|
8月前
|
Shell Linux
解决脚本文件无法执行conda命令的问题:CommandNotFoundError: Your shell has not been properly configured to use
使用Linux系统时,有时候希望利用一个脚本执行多条命令来节省时间,其中如果安装有anaconda,需要切换环境或者关闭conda环境,按道理说,在终端里可以通过命令
444 0
|
9月前
|
Shell PHP Windows
php交互式命令行工具window操作系统安装readline扩展函数实现interactive mode enabled到Interactive Shell
php交互式命令行工具window操作系统安装readline扩展函数实现interactive mode enabled到Interactive Shell
61 0
|
PHP Windows
Windows下laravel/lumen中执行phpunit报phpunit: command not found解决办法
Windows下laravel/lumen中执行phpunit报phpunit: command not found解决办法
121 0
Windows下laravel/lumen中执行phpunit报phpunit: command not found解决办法
|
Shell 开发工具 git
easyswoole 更新代码shell
easyswoole 更新代码shell
57 0
|
JavaScript 测试技术 数据安全/隐私保护
Cypress系列(63)- 使用 Cypress.Commands 完成 Custom Commands 自定义命令
Cypress系列(63)- 使用 Cypress.Commands 完成 Custom Commands 自定义命令
225 0
Cypress系列(63)- 使用 Cypress.Commands 完成 Custom Commands 自定义命令
|
Windows
【Ansible】Ansible控制windows插件安装及运行error与解决方法
一、 问:因pip版本问题无法安装kerberos 答:安装提示需要先安装pip升级包 下载pip9.0.1升级包: https://pypi.python.org/packages/b6/ac/7015eb97dc749283ffdec1c3a88ddb8ae03b8fad0f0e611408f196358da3/pip-9.
7497 0
|
Ubuntu Shell 开发工具
Oh-My-Zsh!提高你CLI(Command-line interface )的神奇工具 - Ubuntu教程
原文更新地址 https://github.com/shellhub/blog/issues/25 Oh-My-Zsh!提高你CLI(Command-line interface )的神奇工具 - Ubuntu教程 1_dcfi6nexitpjcfpgdloonw 我是命令行界面的忠实粉丝.
2115 0

热门文章

最新文章