Simple Twig Fallback

Love the simplicity of Twig; mainly that it allows us to do away with the conditionals that tend to litter most interface code.  You know the drill: If this then show that otherwise show the other thing except in such-and-such a case..  Confusing and brittle.  Twig’s hierarchical layout is the way to go.

Falling back to a parent block depending on complex output can be tricky however.  Here’s a simple way to do it without a bunch of ifs:

Yay. If less.

Or with one ternary conditional if running parent() through default() is unclear:

{{ details|trim|raw ?: parent() }}

ElasticSearch Delete All

Even though it’s not documented anywhere (as far as I can tell), I probably should have realized this would work:


$ curl -XDELETE http://localhost:9200/
{"ok":true,"acknowledged":true}

Deletes all indices in one fell swoop. Nuclear option. Use caution.

Defeating mysterious “This form should not contain extra fields” errors in Symfony Forms

I love the Symfony framework, but really hate the form component at times. Two years after the initial release of Symfony 2 and we’re still waiting on some decent form validation debugging.

But enough griping. If you’ve ever worked with form events, then undoubtedly you have run into the This form should not contain extra fields error. Basically what it means is that the request parameters don’t match up with the form against which you’re trying to validate.

“Yes, but.. but.. which parameters??” I hear you ask.  Try this:

 

Those “extra data” parameters.

 

Not Defined and Not Empty/Null in Twig

Long time no write.  Came across some syntax today that solves an old annoyance in Symfony's Twig.

Rather than blather on in code with the following complex statement to make sure that a value is both defined and not null:

{% if var is defined and var is not null %}

one can instead simply do this:

{% if var|default is not empty %}

or even more simply:

{% if var|default %}

Nice.

Ubuntu’s “upstart” versus old school “update-rc.d”

Every distro seems to have their own damn way of managing startup services. One of these days I should really sit down and figure out how runlevels work.

Or maybe I’ll continue to ignore them since Ubuntu’s simplified (and fairly intuitive) upstart seems to be make it easier to not worry about levels.

Anyway, since Ubuntu is a bit schizophrenic these days with some services using upstart and others not, here are some handy commands for adding/removing startup services. Comments welcome since this is the kind of thing I do only very rarely.

initctl

$ initctl list shows a quick list of startup services. I’m not really sure what’s happening under the hood here, which is why it’s often useful to…

update-rc.d

…see what’s starting at various runlevels. Try this:

$ ls /etc/rc?.d

To see if amavis is starting on boot we can:

$ ls /etc/rc?.d | grep amavis
K21amavis
K21amavis
K21amavis
K21amavis
K21amavis
K21amavis
K21amavis

And since we don’t need it:

$ sudo update-rc.d -f amavis remove
Removing any system startup links for /etc/init.d/amavis ...
/etc/rc0.d/K21amavis
/etc/rc1.d/K21amavis
/etc/rc2.d/K21amavis
/etc/rc3.d/K21amavis
/etc/rc4.d/K21amavis
/etc/rc5.d/K21amavis
/etc/rc6.d/K21amavis

What do you mean “Failed saving metadata to metadataCache”?

Seeing this in Zend? It’s probably an issue with your memcached connection. Check settings in application.ini, namely:

resources.cache.core.backend.options.servers.host = "127.0.0.1"
resources.cache.core.backend.options.servers.port = 11211

and make sure you can connect to where it’s pointing:

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 9528
STAT uptime 374
STAT time 1344441815
STAT version 1.2.6
...
END

Similar to the zend_mm_heap corrupted thing.

Defeating “Maximum function nesting level of ‘100’ reached” problems when running PHPUnit against Symfony2

In a nutshell:  It’s an xdebug config problem.  Try increasing xdebug’s max nesting level in php.ini:

xdebug.max_nesting_level=1000

If you’re on a platform that supports separate php.ini files for both Apache and the command line, be sure you update the cli instance.

This little beauty has already stumped me twice. Thank goodness for Cordova’s obscure bit of advice.

TaskTrayApplication Crash

Do you get an annoying “TaskTrayApplication” crashed or some such warning when Windows 7 boots?  I did too.  Finally took a minute to track it down and it turns out to be HP’s Toner Cartridge Authentication program. I’m pretty sure this program does little more than spy on you, so just remove it from Control Panel -> Uninstall a Program and the problem will go away.

BTW, why are huge tech companies like HP unable to maintain extranets with normal-looking URLs for human beings?  Having to surf through pages like http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?&objectID=c02632486 is ridiculous.

Connecting to SQL Server via JDBC

Every few years I find myself backed into working with SQL Server for something or other. And I can never remember how to wire it up to the various Java-based tools I have. So this time I’m going to stuff some notes into my trusty old backup brain here such that I will be ready to go two years from now.

Version

As of the time of this post, SQL Server 2012 is out.  And it is completely useless to me since the Java world is blissfully unaware that Microsoft, too, has moved on from 2008. Soooo.. if you want to find a free version of SQL Server that actually works on Windows 7, see here: http://www.microsoft.com/en-us/download/details.aspx?id=29846.  Get one with “WT” (for “With Tools” of course) in the title. WT versions include the SQL Server Management Studio via which you will configure SQL Server to not ignore the non-Microsoft world.

Drivers

Lots of JDBC drivers out there.  Here’s a bunch of themjTBS is an open source pure Java type 4 JDBC 3.0 driver that seems to be the best: It supports both Windows and SQL server authentication; even Microsoft’s own drivers only support SQL Server username password auth.  Note that you may need to perform a DLL rain dance as outlined here to get jTBS to play nicely with Windows’ baked-in security.  Peruse the README.SSO packed away inside the jTBS archive.  If you only need username/pass authentication, then read on guilt-free.

Convince SQL Server to Listen

  1. Turn on TCP/IP.  Do this form the “SQL Server Configuration Manager” app.  Select “Network Configuration”, click on your instance, change TCP/IP option to “Enabled”.  Restart SQL Server from Services.
  2. But wait, there’s more.  Now allow username/password authentication in addition to Windows SSO authentication.  (Many Java apps don’t seem to like Windows authentication even if your driver is set up to support it.)  Launch the “SQL Server Management Studio”, right click the root node, select “Properties” and then the “Security” tab.  Under “Server authentication” select “SQL Server and Windows Authentication mode”:

  3. Finally, make sure that at least one user is granted access to the server.  For the “sa” (sys admin) user, fire up SQL Server Configuration Manager again and unfold your way to Security -> Logins -> sa.  Right click, “Properties”, “Status”.  Permission to connect to the database engine:  Grant.  Login:  Enabled.  Okay.Don’t forget to re-restart SQL Server after all of this.

Whew!  Should you have followed the above steps and thrown a pinch of salt over your shoulder then please prepare for a jig-induced euphoric hangover of joy because you, my friend, have successfully connected to SQL Server via JDBC.

If not, find some more salt.