Archive for the ‘ Symfony 2.x ’ Category

I’ve prepared something very useful for you today. Setting a default value. For any relational databases it is very common practice to set some default value if user doesn’t specify any value. For example if we have boolean field and we don’t set default value, in the records we will find one of those values: true, false, null. And there is a question – what does null mean for that column? Is it the same thing as false? Or should we handle nulls in some other way? If you don’t mean to have 3 types of values you should set the default value. This practice is also useful when in the beginning this field will always have the same value an it will be changed any time later.
Database is just one of the layers. When you are using a framework like Symfony 2 you have other layer which is ORM. In this particular case it is Doctrine 2. You should also set the default value there. You can do it by adding a row

"default" = 0

in options argument when declaring an ORM column. For example, it can look like this:

@ORM\Column(
    name="foo",
    type="decimal",
    precision=7,
    scale=2,
    options={"default" = 0}
)
Author : beriba | 12/01/2012

Quick solution to a problem that took me a while to resolve. It’s very simple but I couldn’t find the solution just by asking uncle google. In my case I was trying to execute

php app/console doctrine:schema:update --dump-sql

and it responded me with big red exception

[Doctrine\DBAL\DBALException]
Unknown column type "json" requested. Any Doctrine type that you use has to
be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list
of all the known types with \Doctrine\DBAL\Types\Type::getTypeMap(). If this 
error occurs during database introspection then you might have forgot to 
register all database types for a Doctrine Type. 
Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types 
implement Type#getMappedDatabaseTypes(). If the type name is empty you might 
have a problem with the cache or forgot some mapping information.

First thing to do in that case is to install Sonata Doctrine Extensions in your symfony. There are three steps to do this:
1. Add those lines to file deps in symfony root directory:

[SonataDoctrineExtensions]
    git=https://github.com/sonata-project/sonata-doctrine-extensions.git
    target=/bundles/Sonata/DoctrineExtensions

2. Execute command:

php bin/vendors install

3. Change app/autoload.php file. Your Sonata section shoud look like this

// app/autoload.php
//...
'Sonata' => array(
                __DIR__.'/../vendor/bundles',
                __DIR__.'/../vendor/bundles/Sonata/DoctrineExtensions/src'
            ),
//...

This will install Sonata Doctrine Extensions.

Second thing to do to get rid of the main problem – check if your app/config/config.yml file look like this:

# app/config/config.yml
#...
doctrine:
    dbal:
        #...
        types: #this is about this line and line below
            json:     Sonata\Doctrine\Types\JsonType
#...

This should do the work.