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. 由于命令定义的灵活性,非内部人员不知道你的命令行的格式,也不知道其实现何种操作。
目录
相关文章
|
1月前
项目打包报错“caniuse-lite is outdated. Please run next command `npm update`”的解决方案
项目打包报错“caniuse-lite is outdated. Please run next command `npm update`”的解决方案
39 1
|
5月前
|
数据安全/隐私保护
Mac平台出现brew command not found问题解决方法
Mac平台出现brew command not found问题解决方法
162 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环境,按道理说,在终端里可以通过命令
453 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解决办法
|
Python
【hacker的错误集】(Try to run this command from the system terminal. Make sure...)
今天,闲着没事干,把自己的python和pycharm卸载重安了,结果在安装requests库时报错了…(当时很慌,没有截图)只知道报错内容是
359 0
【hacker的错误集】(Try to run this command from the system terminal. Make sure...)
|
JavaScript 测试技术 数据安全/隐私保护
Cypress系列(63)- 使用 Cypress.Commands 完成 Custom Commands 自定义命令
Cypress系列(63)- 使用 Cypress.Commands 完成 Custom Commands 自定义命令
225 0
Cypress系列(63)- 使用 Cypress.Commands 完成 Custom Commands 自定义命令
Cypress系列(85)- Cypress.platform 命令详解
Cypress系列(85)- Cypress.platform 命令详解
87 0
Cypress系列(85)- Cypress.platform 命令详解
|
JavaScript 前端开发 Go
Cypress - 命令大全
Cypress - 命令大全
679 0