Find Mongo Document By ID Using The PHP Library

My new job as a Developer Advocate with IBM means I get to play with databases for a living (this is the most awesome thing ever invented, seriously). On my travels, I spent some time with MongoDB which is a document database – but I ran into an issue with fetching a record by ID so here’s the code I eventually arrived at, so I can refer to it later and if anyone else needs it hopefully they will find it too. Continue reading

Heroku “No app specified”

I’ve been having a maddening problem where one (but only one) of my heroku apps doesn’t know which heroku app it is, which means I need to append --app app-name to every single command. It seemed to happen when I moved my app to an organisation rather than having it on my personal account, but in fact the problem was that at the same time I did that, I set up the build server to deploy it – and so I removed the old heroku git remote and then never added the new one because I exactly shouldn’t be pushing to heroku from my laptop as we now deploy via Jenkins.

I was looking for some config file or something that heroku would read but what it actually does is look at whether any of your git remotes are heroku and if so, assume by default that you mean that project! The git URL is on the “Settings” screen from the Heroku web interface, and you just need to add it as a remote to your local project:

git remote add heroku [paste git url from settings screen]

Hopefully this helps someone else stop having to type --app app-name every time they need to do something with their app, it was a tiny problem but quite an annoying one!

MySQL 5.7 Introduces a JSON Data Type

There’s a new JSON data type available in MySQL 5.7 that I’ve been playing with. I wanted to share some examples of when it’s useful to have JSON data in your MySQL database and how to work with the new data types (not least so I can refer back to them later!)

MySQL isn’t the first database to offer JSON storage; the document databases (such as MongoDB, CouchDB) work on a JSON or JSON-ish basis by design, and other platforms including PostgreSQL, Oracle and SQL Server also have varying degress of JSON support. With such wide adoption as MySQL has, the JSON features are now reaching a new tribe of developers. Continue reading

Simple Access Control for CakePHP3

The newest version of CakePHP doesn’t ship with built in ACL, which means you need to write your own. Personally I think this is a smart move, having looked at the one-size-fits-all solutions for previous versions of the framework and knowing that every system has different requirements, this version has good hooks and documentation on how to add something that works for your application. I thought I’d share what worked for mine. Continue reading

Change Form Input Type in CakePHP3

I’ve been having my first experiences with generated code, generating a new admin backend using CakePHP3 (yes CakePHP is still around, it’s alive and doing rather well in fact!). So far it’s going great and producing a much more complete solution than I’d have managed for myself on this timescale.

One thing is bothering me though: it guesses form input types from the database column types, which mostly works well but sometimes it picks something that doesn’t reflect the way that the user will store information in this field. It’s actually pretty easy to change the forms that get generated though, so here’s an example. Continue reading

Documentation First: A Recipe for API Success

I’ve shipped a handful of greenfield APIs in recent months for different clients, and in each case I’ve been building the documentation before the API. I hadn’t really recognised it as a pattern until someone else commented on it, but I do find this approach has worked well for my projects, so I thought I’d share my thoughts on this in a bit more detail. Continue reading

PHP 7.0 (and 5.6) on Ubuntu

PHP 7 is released but for those of us who don’t usually compile our own PHP, it can be a long wait for our preferred distro to release the packages we want. For Ubuntu, I’m using a PPA which allows both PHP 5.6 and PHP 7.0 to be installed, including things like extensions, at the same time. It was very easy to set up (I’m running Ubuntu 15.10 but this process should also work on older versions back to at least 14.04 which is the previous LTS) so here’s a quick walkthrough of what I did. Continue reading

Simple One-to-one Meetings

Recently I was giving some advice (that I was asked for, which is novel) regarding one-to-one meetings between developers and either team leads or management can be structured. My thoughts really boiled down to some very short points (this is why sometimes, those meetings take 15 minutes and other times they take 3 times that for a monthly update!). In case they’re useful to anyone else, here’s my meeting outline:

  • What’s going well/what are you excited about?
  • What’s tedious/annoying or actually a problem?
  • What could I be doing that I’m not?

Continue reading

Use Ngrok for Testing APIs on Dev

Recently I was hastily building an API for a client and I wanted to run some tests against it. I’ve written before about using Runscope for API testing, but this was against a local dev platform (inside a VM, not directly on my laptop) rather than a public API. The same problem arises if you want to access a local site or API from elsewhere or from a mobile device. In all these scenarios, ngrok is your friend. Continue reading

Handling Composer “lock file out of date” Warning

Composer is dependency management for PHP, and it consists of two main files:

  • composer.json where you specify your dependencies
  • composer.lock where composer itself records exactly which precise version of every library and every dependency of every library it picked, so all installs will be identical

Crucially, the composer.lock also includes a hash of the current composer.json when it updates, so you can always tell if you’ve added a requirement to the composer.json file and forgotten to install it. Continue reading