Tuesday, November 27, 2012

CSS, Sublime Text 2, Colon, Vintage Mode

Can't use ':w' to save CSS/LESS files in ST2 in Vintage mode because it keeps just inserting the colon? The problem is actually with ZenCoding keybindings.

Quick Fix

Add the following to your User Confirmation file, found via Preferences -> Key Bindings (User)


{ "keys": [":"], "command": "show_overlay", "args": {"overlay": "command_palette", "text": ":"},
        "context": [{"key": "setting.command_mode"}]
    }

Note: The above should be inserted inside of the square brackets ([]).

What's going on?

ZenCoding has a default key binding for ':' that doesn't really respect Vintage mode. You can see these bindings in Preferences->Package Settings->ZenCoding->Bindings->Key Bindings (Default).

The binding mentioned in the Quick Fix is a copy/paste from the Vintage package key bindings. What we're doing here is putting it into our User key binding file, effectively over-ridding the one from ZenCoding.


Sunday, November 11, 2012

Using services.yml instead of services.xml in Symfony 2

In my application's bundle, I recently wanted to switch from using an XML file to register my services to a YML file. There's more information about services here: http://symfony.com/doc/current/book/service_container.html

Here's how to do it.

Create the services.yml file in your bundle, probably in something like /src/Acme/DemoBundle/Resources/config/services.yml

Next, edit the bundle's extension file and change which services file gets loaded:


// src/Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php

namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;

class AcmeDemoExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }

    public function getAlias()
    {
        return 'acme_demo';
    }
}


The main changes are:

  1. switching the XmlFileLoader to YamlFileLoader in line 14
  2. canging the file to services.yml in line 15

Saturday, November 10, 2012

Symfony 2: Drop, Create, and Update Database

The Problem

This is regarding symfony 2.1 CLI tasks. When trying to add a task that dropped, created, and updated my database, I received the following:


[Doctrine\DBAL\DBALException]                                                              
An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':  
                                                                                           
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected                           
                                                                                                                                                                 
[PDOException]                                                    
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

The problem, it turns out, was that doctrine was using the same connection that it used to drop the database as it was to update the database. After the drop, doctrine lost it's awareness to your database and did not re-establish it upon create.

The Fix

You need to close the connection after calling doctrine:database:drop and before doctrine:database:update --force

$connection = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getConnection();

  if ($connection->isConnected()) {
   $connection->close();
  }


And just for reference:

Drop, Create, and Update database in Symfony 2 CLI Task



  $drop = $this->getApplication()->find('doctrine:database:drop');
  $drop_args = array(
   'command'=>'doctrine:database:drop',
   '--force'=>true
   );

  $drop->run(new ArrayInput($drop_args), $output);

  //Make sure we close the original connection because it lost the reference to the database
  $connection = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getConnection();

  if ($connection->isConnected()) {
   $connection->close();
  }

  $create = $this->getApplication()->find('doctrine:database:create');
  $created_args = array(
   'command'=>'doctrine:database:create'
   );

  $create->run(new ArrayInput($created_args), $output);

  $update = $this->getApplication()->find('doctrine:schema:update');

  $update_args = array(
   'command'=>'doctrine:schema:update',
   '--force'=>true,
   '--em'=>'default'
   );

  $update->run(new ArrayInput($update_args), $output);