Symfony ChoiceType Default to Checkbox Checked

I’m rendering a bunch of checkboxes using a Symfony form type of ChoiceType and I want some of them to be checked by default.

It’s basically a list of transactions and the ones that fail will be checked so that the user can fix whatever caused the problem and run them failed ones again. It would be tedious to make them check what could be 100s of checkboxes to rerun the failures.

$results = $this->getJobResults($job);
$transactions_array = $data = array();
foreach ($results as $result) {
  if ($result->success != 'Success') {
    $data[] = $result->id;
  }
  $transactions_array[$result->id] = $result->id;
}
$retryForm->add('transactions', ChoiceType::class, array('multiple'=>TRUE, 'expanded'=>TRUE, 'choices'=>$transactions_array, 'data'=>$data));

What I needed to do was build the $data array to contain the same values in $transactions_array (choices) of the checkboxes I want checked. When the form is rendered, if the checkbox value is in the $data array, it’s checked by default. For some reason that wasn’t obvious in the Symfony documentation.

Bosch Dishwasher E15 Error Message

screen-shot-2016-10-31-at-10-32-32-am
This post is off-topic, but I think it’s useful information for the world to know. I have a Bosch dishwasher and a few weeks ago it started failing with an E15 error message.

The E15 Bosch error message is related to a feature of the dishwasher to prevent it from flooding. So if the pump fails or something, rather than spilling water all over your kitchen floor, it basically shuts down. While researching the Bosch E15 error message, I found lots of different reasons for this message. Some of which are simply that the dishwasher is dirty and that filters inside it should be cleaned out. That wasn’t my situation. Some will say, pull the dishwasher out, angle it at 45 degrees to drain excess water out of the machine. While that will “work,” it doesn’t solve the underlying problem that is causing the dishwasher to go into E15 flood mode.

In my case, the issue appears to have been related to how the dishwasher was installed. The dishwasher drain hose has to wind its way through my cupboards to get to the sink drain. In doing so, the drain hose ran all the way up to the underside of the countertop, then dropped back down to get to the drain. That huge arc was apparently way too much for the little pump in the Bosch dishwasher.

I didn’t install the dishwasher, it was here when I bought the house a few months ago. So I pulled everything apart and rerouted the hose to go pretty much straight over to the drain and eliminated the big arc. I’ve been running it for about a week without any E15 error messages.

So if you get an E15 error message on your Bosch, check your hoses. I don’t know this for a fact, but I’d keep the hose running below where it hooks up to the drain. And I’d certainly avoid huge arcs in the hose. While it did run fine for quite awhile with my old hose setup, my guess is that the pump isn’t as strong as it was when it was new, and it couldn’t push the water over that hump, and so it was backfilling into to the machine and causing the E15 Bosch error.

How to Truncate a File from Command Line on Mac or Linux

screen-shot-2016-10-24-at-10-13-13-amI often get data files with a ton of data, but I only need the first few lines. It’s usually because I’m building out some test data for code I’m writing. I don’t need to create 2,000 accounts to run my code.

Here’s how I figured out how to truncate a file from the command-line on Mac.

sed -i '' '161,$ d' data.csv 

You don’t need the -i parameter ” if you’re on Linux.

sed -i '161,$ d' data.csv

The -i stands for in-place. The 161 is the line number to begin truncation. The $ means, truncate to the end of the file. Obviously, data.csv is the data file.

Symfony Twig Call Object Method in Array using Variable as Array Key

I have an an array of objects that I’ve passed into a Symfony Twig template. I actually have several hashes of objects that I’m passing in that are all keyed the same way. When looping one of these hashes, I need to pull in other bits of data from the other hashes. So I needed to be able to fetch an object from one of the other hashes and call one of its methods.

To do it with Twig was easy.

Imagine there’s a hash of Transaction objects keyed with a collatorId and you want to check if it’s been processed or not while looping through a hash of Requests, also keyed with the same collatorIds.

{% for request in requests %}    
  {% if transactions[request.collatorId].processed != TRUE %}
  NOT PROCESSED
  {% else %}
  PROCESSED
  {% endif %}
{% endfor %}

That will call the processed method for the transaction with a collatorId of request.collatorId.

Updating Ubuntu 14.04 Reverts Command-Line PHP to Version 7

Moto Cambodge Phnom Penh, Cambodia

Moto Cambodge Phnom Penh, Cambodia

PHP version 7 looks really cool and I can’t wait to start using it, but for now, I’m still on PHP 5.6. But every time I do an update on my Ubuntu dev box, it sets the PHP command-line to PHP version 7.

Here’s how to switch it back on Ubunut 14.04.

ln -sfn /usr/bin/php5.6 /etc/alternatives/php

Symfony Load Twig Template From String

As is always the case with PHP, or open source technology, there’s a gazillion ways to rule the world, or solve a problem. I had a problem with a Symfony app I was building. I needed to load the Twig template from a string variable rather than a file.

To do so is easy. All you have to do is activate the Twig_String_Loader extension and use template_from_string to load it.

I’m dynamically creating a SOAP request, inserting the proper user credentials on the fly. To do that I created a template called soap-request with this in it.

{{ include(template_from_string(xml)) }}

Before that can work, the Twig_String_Loader needs to be loaded. You can do that in app/config/services.yml, or wherever you configure services.

my.twig.extension.loader:
    class: Twig_Extension_StringLoader
    tags:
       - { name: 'twig.extension' }

There are other convoluted ways to load the Twig_String_Loader, but this is the cleanest most Symfony-esque way to do it.

In my controller I do something like this.

$options = array(
  'username'=>'doser',
  'password'=>'moser',
  'xml'=>$data['xml']
);

$payload = $this->renderView('MyBundle:Default:soap-request.html.twig', $options);

The $data[‘xml’] is a SOAP request that has some tokens in it like {{ username }} and {{ password }}. In soap-request.html.twig, the include(template_from_string(xml)) handles variable interpolation. Then $payload contains a the SOAP request I want with the username and password set.

MariaDB Corp. Slaps Open Source Community in the Face

Screen Shot 2016-08-22 at 11.39.14 AMA few years ago, Oracle acquired MySQL, the open source community supporting MySQL proverbially self-evacuated, forked MySQL and start MariaDB. The heroes saved MySQL from the death of then-CEO Larry Ellison. Now MariaDB has gone commercial.

On Auguest 15, Michael Widenius announced the next version of the MaxScale would not be open source. Widenius, creator of MySQL, came up with the Business Software License a couple of years ago and this his is first attempt at implementing it.

Under the BS License, the MaxScale source code will be made open source once it’s obsolete, after a period of time. Before that, if you want to use it, you’ll be paying for it.

So thanks for all of the hard work open source community, we’ll take it from here. Cha-ching? Or ka-boom?

Software Engineering: Work Your Code

I see too often code written by others that is not finished but running as production code. The programmer wrote the code, it “worked,” and she moved on to making the next thing “work.” Even for people like myself who have been writing code for nearly two decades, the first draft is just that – a draft.

I look at writing code like carving a statue from block of stone. The stone is the blank screen in your editor with the power of your preferred programming language waiting to start carving into it. The first few whacks at it will leave the stone still looking like a rock rather than the figure of beautiful mermaid swimming through a coral reef. That’s the same with your code. The first few attempts at making it “work,” are just the initial hammer strikes.

To build professional software that is polished and impressive, requires many iterations and much cajoling. The more experience you gain as a programmer, fewer iterations will be required.

And like an artist, a programmer needs to step back and look at the entire application and the systems it interacts with and see where things may be askew.

As the programmer rolls over the code, again and again, it begins to take shape and become coherent software that makes sense to the author and future programmers who will need to work on it and understand what’s going on.

How to Configure Postfix to Use a Relay not on Port 25

Sending email over port 25 is very 1990s. Most of us are running our email servers on cool ports like 587. Well for some reason I really stumbled getting Postfix to relay through my SMTP server for a new development environment. I’ve done this at least a half-dozen times, but I never had this difficult of a time getting to work.

The problem was that I wasn’t configuring the relay in the main.cf and the sasl_passwd correctly.

How I got it to work was like this.

In main.cf

relayhost = [mail.example.com]:587

And in the sasl_passwd it looks like this.

[mail.example.com]:587 username:password

PHP SoapClient Create An Array of Objects

In PHP, you can’t create an array with the same key more than once. You can’t do this.

$array['transaction'] = "success";
$array['transaction'] = "fail";

Obviously, you can DO that, but “fail” will overwrite “success.”

But when making Soap calls, it’s often necessary to pass in a structure like this.

<users>    
    <user>
    <firstname>Jamal</firstname>
    </user>
    <user>
    <firstname>Acacia</firstname>
    </user>
</users>

But you can’t create an array or an object in PHP that looks like that. Well, you can but it needs to look like this.

$request = (object)array('users'=>(object)array('user' = array(
  (object)array('firstname'=>'Jamal'),
  (object)array('firstname'=>'Acacia')
)));

I think you can do it without casting to objects, but in SOAP, objects more closely map to SOAP structures than arrays or hashes do, in my opinion.