How to for SQLite

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

How to for SQLite

Postby langis » Wed Jun 01, 2011 4:44 pm

I have an SQLite database that I would like to use to build my schema and generate entities, but I cannot find accurate info on doing it. Where should the database be, how should the configuration point to the database?

This seems relatively simple for mySQL and other databases, but not so for SQLite.

Thanks for any help.
User avatar
langis
Junior Member
 
Posts: 23
Joined: Thu Feb 23, 2006 5:55 am

Re: How to for SQLite

Postby jfgrissom » Fri Jun 03, 2011 7:54 am

Hi Langis,

Here is some information on doctrine with some items pointing to SQLite.
http://www.doctrine-project.org/blog/do ... rm-sandbox

I've not done this, but I would imagine:
1) You need to be sure SQLite PDO is available to your PHP server.
2) Configure symfony to use SQLite (This is what I found, using Propel and Symfony 1.2 with SQLLite. It might not be too different with symfony2.)
2.a) Read this to configure your driver (http://symfony.com/doc/current/book/doctrine/dbal.html).
2.b) I imagine you just need to change pdo_mysql to pdo_sqlite in your app/config/config.yml but I'm not 100% sure.
2.c) You will likely need to define a DSN or Database where you actually point it to any directory you wish to store your SQLite DB in. (See the link to the 1.2 SQLite question above.)
3) Then the rest as usual, build entities and manage them through your entity manager.

I believe you'll have to finagle it out the details for yourself but this should be doable... the Symfony2 DBAL should allow for this with no problems.

Hope it helps!
:D~:-D~:grin:
The Principle Of Antagonism:
A protagonist and his story can only be as intellectually fascinating and emotionally compelling as the forces of antagonism make him.
---Robert McKee
http://jfgrissom.wordpress.com
User avatar
jfgrissom
Faithful Member
 
Posts: 226
Joined: Tue Jan 25, 2011 7:24 pm
Location: San Diego

Re: How to for SQLite

Postby langis » Wed Jun 08, 2011 3:50 pm

Thanks JFGrissom,

I have not looked into what you referred me yet, but here is a few things I found out:
1) in order to use sqlite, the Driver has to be "pdo_sqlite" and not PDOSqlite as some other forum on the subject claimed.
2) second, in your parameters.ini you need to configure a variable called database_path, because Sqlite does not use the database_name variable, but is expecting the database_path:
Code: Select all
;app/config/parameters.ini
[parameters]
    database_driver        = pdo_sqlite
    database_host         = localhost
    database_name       = databaseName
    database_path         = %kernel.root_dir%./databaseFilename.db
    database_user         = root
    database_password = password

3) You must also modify the config.yml to add the path variable as follows
Code: Select all
  doctrine:
    dbal:
      driver:       %database_driver%
      host:        %database_host%
      dbame:     %database_name%
      path:        %database_path%
      user:        %database_user%
      password: %database_password%

I believe that you must have a similar section in config_dev.yml if you wish to use a different database for development than production.

I was trying to use the 'php app/console doctrine:mapping:import myTestBundle yml' and it appropriately created the mappingFiles.yml files for my database once I did the steps above.
And, when I try to use the command 'php app/console doctrine:generate:entities' command, by putting debugging code in the source Symfony packages involved, it uses the className 'Acme\myTestBundle\Entity\Acme\myTestBundle\Entity\Tablename' instead of 'Acme\myTestBundle\Entity\Tablename'. When I force-fed the proper className in the Symfony code, it refuses to generate any 'getter' and 'setter' functions for any attributes but completes the process without reporting any errors and the resulting className.php file only have the 'id' fields declared as properties for the classes, none of the other fields appear.

I will try to report more as I work through this issue. I should state that when I used a 'mySQL' database, it worked as expected without any of the issue I met with Sqlite.
User avatar
langis
Junior Member
 
Posts: 23
Joined: Thu Feb 23, 2006 5:55 am

Re: How to for SQLite

Postby langis » Thu Jun 09, 2011 7:06 pm

I have installed Symfony Beta4, and here is my parameters.ini:

Code: Select all
[parameters]
    database_driver   = pdo_sqlite
    database_host     = localhost
    database_name     = SplitIncome
    database_path     = %kernel.root_dir%/Data/SplitIncome.db3
    database_user     = root
    database_password =



Here is my Doctrine configuration in config.yml:
Code: Select all
# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        dbname:   %database_name%
        path:     %database_path%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true



Here is my Autoload.php:
Code: Select all
<?php

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    'Sensio'           => __DIR__.'/../vendor/bundles',
    'JMS'              => __DIR__.'/../vendor/bundles',
    'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    'Doctrine\\DBAL'   => __DIR__.'/../vendor/doctrine-dbal/lib',
    'Doctrine'         => __DIR__.'/../vendor/doctrine/lib',
    'Monolog'          => __DIR__.'/../vendor/monolog/src',
    'Assetic'          => __DIR__.'/../vendor/assetic/src',
    'Metadata'         => __DIR__.'/../vendor/metadata/src',
    'Acme'             => __DIR__.'/../src',
    'test'             => __DIR__.'/../src',
));
$loader->registerPrefixes(array(
    'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
    'Twig_'            => __DIR__.'/../vendor/twig/lib',
));
$loader->registerPrefixFallbacks(array(
    __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs',
));
$loader->registerNamespaceFallbacks(array(
    __DIR__.'/../src',
));
$loader->register();

// Swiftmailer needs a special autoloader to allow
// the lazy loading of the init file (which is expensive)
require_once __DIR__.'/../vendor/swiftmailer/lib/classes/Swift.php';
Swift::registerAutoload(__DIR__.'/../vendor/swiftmailer/lib/swift_init.php');


Here is my AppKernel.php:

Code: Select all
<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\ClassLoader\DebugUniversalClassLoader;
use Symfony\Component\HttpKernel\Debug\ErrorHandler;
use Symfony\Component\HttpKernel\Debug\ExceptionHandler;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
            new Acme\StudyBundle\AcmeStudyBundle(),
            new Acme\TestBundle\AcmeTestBundle(),
            new test\SplitIncomeBundle\testSplitIncomeBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Symfony\Bundle\WebConfiguratorBundle\SymfonyWebConfiguratorBundle();
        }

        return $bundles;
    }

    public function init()
    {
        if ($this->debug) {
            ini_set('display_errors', 1);
            error_reporting(-1);

            DebugUniversalClassLoader::enable();
            ErrorHandler::register();
            if ('cli' !== php_sapi_name()) {
                ExceptionHandler::register();
            }
        } else {
            ini_set('display_errors', 0);
        }
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }
}


Here is the result of the commands I issue:
> php app/console init:bundle test/SplitIncomeBundle src
Summary of actions
- The bundle "testSplitIncomeBundle" was created at "src/test/SplitIncomeBundle" and is using the namespace "test\SplitIncomeBundle".
- The bundle contains a sample controller, a sample template and a sample routing file.

Follow-up actions
- Enable the bundle inside the AppKernel::registerBundles() method.
Resource: http://symfony.com/doc/2.0/book/page_cr ... the-bundle
- Ensure that the namespace is registered with the autoloader.
Resource: http://symfony.com/doc/2.0/book/page_cr ... on-sidebar
- If using routing, import the bundle's routing resource.
Resource: http://symfony.com/doc/2.0/book/routing ... -resources
- Starting building your bundle!
Resource: http://symfony.com/doc/2.0/book/page_cr ... mfony-page

> php app/console doctrine:mapping:convert yml ./src/test/SplitIncomeBundle/Resources/config/doctrine/metadata/orm --from-database --force
Processing entity "Bracket"
Processing entity "Couple"
Processing entity "Income"
Processing entity "Individual"
Processing entity "Tax"

Exporting "yml" mapping information to "E:\.....\src\test\SplitIncomeBundle\Resources\config\doctrine\metadata\orm"

> php app/console doctrine:mapping:convert xml ./src/test/SplitIncomeBundle/Resources/config/doctrine/metadata/orm --from-database --force
Processing entity "Bracket"
Processing entity "Couple"
Processing entity "Income"
Processing entity "Individual"
Processing entity "Tax"

Exporting "xml" mapping information to "E:\.....\src\test\SplitIncomeBundle\Resources\config\doctrine\metadata\orm"

> php app/console doctrine:mapping:import testSplitIncomeBundle annotation
Importing mapping information from "default" entity manager
> writing E:\.....\src\test\SplitIncomeBundle/Entity/Bracket.php
> writing E:\.....\src\test\SplitIncomeBundle/Entity/Couple.php
> writing E:\.....\src\test\SplitIncomeBundle/Entity/Income.php
> writing E:\.....\src\test\SplitIncomeBundle/Entity/Individual.php
> writing E:\.....\src\test\SplitIncomeBundle/Entity/Tax.php

> php app/console doctrine:generate:entities testSplitIncomeBundle
Generating entities for bundle "testSplitIncomeBundle"


[ErrorException]

Notice: Undefined index: Acme\StudyBundle\Entity\Acme\StudyBundle\Entity\bracket in E:\.....\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\AbstractFileDriver.php line 121



doctrine:generate:entities [--path="..."] [--no-backup] name


>


Note that the Notice above reflects the error as the index has Acme\StudyBundle\Entity\Acme\StudyBundle\Entity\bracket as opposed to test\SplitIncomeBundle\bracket. The repeating part in italic above was my original problem, but also the fact that it refers to the AcmeStudyBundle as opposed to the testSplitIncomeBundle. What is the source of this error?
After deleting the content of the cache and issuing the php app/console cache:clear command, here is what I get from running the generate:entities command:

>php app/console doctrine:generate:entities testSplitIncomeBundle
Generating entities for bundle "testSplitIncomeBundle"



[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "@Table" in class test\SplitIncomeBundle\Entity\Bracket was never imported.



doctrine:generate:entities [--path="..."] [--no-backup] name


Thanks
User avatar
langis
Junior Member
 
Posts: 23
Joined: Thu Feb 23, 2006 5:55 am


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Justinjmi and 4 guests