How to use sqlite database on symfony2 project?

前端 未结 5 1216
温柔的废话
温柔的废话 2020-12-29 05:14

In a Symfony2 project, you can configure the databases connections at the app/config/parameters.ini file. Documentation states that you can use, among others, sqlite3 PDO dr

相关标签:
5条回答
  • 2020-12-29 05:42

    I've found that if I add a path line pointing at the database_name to my config.yml, sqlite seems to pick that up, and MySQL doesn't seem to complain.

    doctrine:
        dbal:
            driver:   %database_driver%
            host:     %database_host%
            port:     %database_port%
            dbname:   %database_name%
            path:     %database_name%
            user:     %database_user%
            password: %database_password%
            charset:  UTF8
    

    This means you can still keep all database information in the parameters file, you don't need separate configs depending on which database you are using.

    0 讨论(0)
  • 2020-12-29 05:47

    According to Doctrine the elements used for sqlite DBAL configuration are:

    • user (string): Username to use when connecting to the database.
    • password (string): Password to use when connecting to the database.
    • path (string): The filesystem path to the database file. Mutually exclusive with memory. path takes precedence.
    • memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.

    This is also listed in the full reference for Doctrine configuration in Symfony2, although not elaborated on.

    So you need to switch up your config params to match whats appropriate for sqlite.

    0 讨论(0)
  • 2020-12-29 05:50

    Mainly the file path or the file path permisssion will have issue.

    In config.yml, set path to full path like

    /home/{name}/NB/PHP/Symfony/test/src/Database/data.db3

    Dont give %database_path% or what ever. Try this it will work.

    If it works you can give as

    %kernel.root_dir%/../src/Database/%database_path%

    Also check sqlite is ok by

    phpinfo(INFO_MODULES);

    In view/output you can see pdo_sqlite and its version.

    0 讨论(0)
  • 2020-12-29 05:54

    In my case setting a username and password in config/packages/doctrine.yaml did not create a username/password protected sqlite database.

    doctrine:
        dbal:
            charset:  UTF8
            url: '%DATABASE_URL%'
            user:     'foo'
            password: 'bar'
    

    It seems like the parameters username and password are ignored?

    0 讨论(0)
  • 2020-12-29 06:03

    Here is what I needed to get SQLite to work, just after doing symfony new myapp :

    in app/config.yml :

    # Doctrine Configuration
    doctrine:
        dbal:
            driver:   pdo_sqlite
            path:     "%database_path%"
    

    In app/config/parameters.yml:

    parameters:
        database_path: "%kernel.root_dir%/db/myapp_%kernel.environment%.db3"
        ...
    

    Next I could do a composer install, create a new entity and it just worked.

    0 讨论(0)
提交回复
热议问题