HarmsSite

Web Development Blog

Abstract Time Stamped Entity in Symfony2

One of my favorite features of Ruby on Rails is that all models are timestamped by default. Ruby on Rails, of course, opts for convention over configuration. Symfony2, on the other hand, opts for configuration over convention. Anyone who has worked with Symfony2 can speak to the mass of config files that exist in a project. I can understand, therefore, why timestamped models (entities) are not a feature in Symfony2. In this post I will show how to add timestamp all your Symfony2 entities. For this post, I chose to use Doctrine as my ORM, so all code show directly relates to Doctrine and Symfony2.

Luckily, adding and updated and created fields to an entity in Symfony2 with Doctrine is quite easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/**
 * @var \DateTime
 *
 * @ORM\Column(name="created", type="datetime")
 */
private $created;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated", type="datetime")
 */
private $updated;

public function __construct()
{
    $this->setCreated(new \DateTime());
    $this->setUpdated(new \DateTime());
}

// getters & setters

/**
 * Auto set the updated date
 *
 * @ORM\PreUpdate
 */
public function setUpdatedValue()
{
   $this->setUpdated(new \DateTime());
}

The code should be pretty self explanitory, we have two DateTime field types that get initially set on entity creation in the contstructor. I then add a setUpdatedValue() function that runs when on PreUpdate of the entity.

The above code works very well, but it is not an ideal solution for working with multiple models. Of course, we could add the updated/created values to every entity, but that wouldn’t be very DRY of us.

Free Giveaway of Administrating Solr

Administrating Solr cover Readers would be pleased to know that I have teamed up with Packt Publishing to organize a Giveaway of the book “Administrating Solr.”

Three lucky winners stand a chance to win a digital copy of the book. Keep reading to find out how you can be one of the Lucky Winners.

Administrating Solr: A Review

Administrating Solr is for developers and Solr administrators who have basic knowledge of Solr and are looking for ways to keep their Solr server healthy and well maintained. Basic working knowledge on Apache Lucene is recommended, but is not mandatory.

Pact Publishing released a new book in October 2013 called Administrating Solr. In the book readers can find out all sorts of information about, you guessed it, administrating your Solr installation. Whether it be Searching Solr, Monitoring Solr, Managing Solr, or Optimizing Solr the book has your bases covered.

At the start, Administrating Solr gets readers familiar with Solr terminology. The Solr Request, Response and RequestHandler are rightfully given their due explanation, as they are the foundation of the Solr configuration. Throughout the rest of Chapter 1 readers can find a plethora of information about searching Solr; whether by query, filter, facet, geospacial, or any combination of the various searching methods, Administrating Solr has readers covered. Chapter 1: Searching Solr reads like a Solr Cookbook when it comes to the different ways to search Solr.

Chapter 2: Monitoring Solr is all about how to set up your own Solr monitoring; monitoring that goes beyond what is presented to users of the Solr Admin. All of what is described is easy to enable/install for those who want deaper Solr monitoring. The chapter finishes with an overview of some of the more popular monitoring tools, intended for those who don’t want to do all the monitoring setup themselves and would prefer to rely on some third party service. Coverered in the Monitoring Tools section are: Opsview, New Relic, and SPM. The author seems partial to SPM and I have only ever used New Relic’s integration, but it seems fair to say that you will be provided very high quality Solr monitoring with any of the listed tools.

The 3rd Chapter: Magaging Solr is all about various scripts you can create to help you manage your Solr isntall better. This chapter again reads like a cookbook, which is very handy if you are looking for infomation about backing up Solr, Solr Replication. Also covered in the chapter is Solr Logging. Solr logging was refigured in Solr 4.3 to be more flexible, so included are some ways to get better logging in your install.

The final chapter, Optimizing Solr Tools and Scripts is by far the best chapter. Before this book I had never heard of such as tool as Drools. This book not only gives readers an understanging of what Drools is, but it also provides an overview of how to use it with real examples. Also covered is setting up Language Detection inside Solr so that the right fields are searched depending on language. Sentence detection and splitting, Tokinization, Part-Of-Speech tagging, and Named entity recognition are all given minor coverage. Each of these topics could be deserving of there own book, so it is understandable that they receive only a paragraph telling the reader what the topic is. The chapter finishes with a case study in Drupal, though the author’s suggestions could be implemented into any platform.

All in all, this book is very useful to those wishing to get their feet wet with Solr. Solr is an incredibly useful search engine capable of some pretty amazing feats. The book is worth it for the last chapter alone. This book was short and to the point and oh so useful. Check it out at Pact Publishing’s store.

South Dakota Code Camp 2013

I would say that South Dakota Code Camp 2013 was another huge success. The organizers were once again able to find some great speakers that cover a wide array of topics. Moreover, this year I had the opportunity to speak. I chose to speak about Django, one of my favorite web frameworks. Anyway, here is a link to my slides.

An Introduction to Django / Tyler Harms

jQuery Animate Body Scroll for All Browsers

It turns out that when trying to use jQuery’s animate to scrollTop on the main body of an html document different browsers require different targets to scroll to. Webkit requires targeting the body of the document while all other browsers that I know of use html. The typical solution is to target both in the same call:

Problematic Callback
1
2
3
4
5
$('html, body').animate({
    scrollTop: '0px','fast', function(){
        // Code to be fired when complete goes here (fired twice)
    }
});

This solution is problematic, however, because even though the scrollTop will only work once, the callback is fired twice. That means that if we were relying on the callback to tell us that the function is complete, our complete function would fire twice. Luckily, there is a relatively simple solution to this problem. Using jQuery’s browser plugin we can have webkit browsers target the body and all others target the html.

Single Callback
1
2
3
4
5
6
7
// If using a newer version (>= 1.9) of jQuery a separate browser 
// plugin is required
$(jQuery.browser.webkit ? "body": "html").animate({
    scrollTop: '0px','fast', function(){
        // Code to be fired when complete goes here
    }
});

This solution worked well for me. I am open to different ideas or solutions if you know of any.

Sort Solr Facets on Per Field Basis

I recently faced a strange problem. I was faceting off of multiple facet.fields and I found the need to sort them independently of each other. As far as I knew up to this point facet.sort could only be set once. When I read the doc, however, it was claimed that the facet.sort could be set on a per field basis; sadly no examples were provided nor was it explained any further. Luckily, after some digging, I found out that setting the sort (index or count) on a per field is very easy.

A typical solr fetch with facets might look like this (assuming type_s is indexed):

1
/solr/directory/select?q=*:*&wt=json&indent=true&facet=true&facet.field=type_s

Solr sets facet.sort to count by default, so that can be left undeclared or can be explicitly declared, it does not matter. A problem arises, however, when you have multiple facet.fields and you want them sorted differently. To solve this you need to follow this format for the declared sort: f.<facetted_field>.facet.sort=<index|count>. Using the same example as before, but with an extra field it would look like this:

1
/solr/directory/select?q=*:*&wt=json&indent=true&facet=true&facet.field=type_s&facet.field=location_s&f.location_s.facet.sort=index

This would sort type_s by count (default) and sort location_s by index. The opposite would also work.

1
/solr/directory/select?q=*:*&wt=json&indent=true&facet=true&facet.field=type_s&facet.field=location_s&facet.sort=index&f.location_s.facet.sort=count

This would change the default sort order to use index for sort and location_s would use count for sorting.

Chrome Webfont Mimetypes

As a web developer whose browser of choice is Google Chome, I use the Chrome console a lot. Chrome is already the best browser out there and always getting better. More than the browsing experience though, I maintain that it has the best dev environment available. It is the little things that really make Chrome so much better than its compeditors (docking to the right for instance). As if you needed any more reasons to like Chrome, the Chrome team just pushed an update to ‘clean up the console.’ This was a fix that did nothing for end user usablity, but fixes that annoying little message telling you about your font mimetypes. This was not a fix that I needed for development, just a nicity that the Chome team provided.

The fix: https://chromiumcodereview.appspot.com/18308003/patch/1/2

IE10 Conditional Comments

Let me start by saying that I was very excited by IE10. Internet Explorer 10 was supposed to be the premier IE web browser. Microsoft was so confident that they created a browser that could meet modern standards that they dropped conditional comments in IE10. If you don’t know anything about developing for IE, just know that it sucks; it was, however, aleviated some by conditional comments that allowed for custom styles/js/whatever for IE versions. My first time working with IE10 I was made harshly aware that conditional comments no longer work. What’s worse, my site that functioned and looked great in IE9 no longer functioned in IE10. How does this happen?

The more I work with IE10 the more issues I find with it, Microsoft needs to bring conditional comments back. Until then, however, there is a simple script you can stick in your javascripts that will identify when IE10 is being used so that you can target it in your css or javascript.

The javascript:

1
2
3
4
5
6
// Add IE10 class to IE10
// $.browser requires jquery < 1.9
// $.browser can be used in jquery > 1.9 with plugin
if ($.browser.msie && $.browser.version === 10) {
  $("html").addClass("ie10");
}

Or if you use coffeescript:

1
2
3
4
5
# Add IE10 class to IE10
# $.browser requires jquery < 1.9
# $.browser can be used in jquery > 1.9 with plugin
if $.browser.msie and $.browser.version is 10
  $("html").addClass("ie10")

It is sad that we must do this for a supposed modern browser; nevertheless, I hope this helps for when you have to work with IE10.

Symfony2 Coding Standards

With eZ Publish moving to Symfony2, I found that I was in need of learning some new coding standards. I never really looked into PHP CodeSniffer while doing eZ Publish development (most of my code was in the templates), so I never really thought about PHP CodeSniffer as an option until starting work with Symphony2. To the point, I just wanted to share the Symfony2 PHP CodeSniffer Coding Standard on Github. It is really easy to install and because phpcs works with PHPStorm and Sublime Text 2, sniffing out php files couldn’t be easier.

New Site Theme

I have been working on getting a new site theme up and running for a while now. My hold up was that I could never commit to a platform. I knew that I wanted off Wordpress and onto something more developer friendly. Originally I was going to put this site on eZ Publish, but that just seemed like overkill. Next I thought about creating a Symfony2 blog, but I wanted to cheapen my hosting options so I opted against Symfony2. I finally decided upon Rails, hosted on the free tier of Heroku.

I was in the middle of development when I discovered Octopress. How I had never heard of this platform before, I don’t know. In under 4 hours total I had my Wordpress site migrated off of Wordpress and onto a Github Pages site generated by Octopress; it was a very smooth transition. Because development was so fast, I opted to stick with Octopress and GH-Pages.

My site is now running statically using Octopress as a site generator and Github as a free host. Octopress is built on Jekyll, so I can make plugins easily (in Ruby) as well as pull in any existing plugin with ease. Octopress seems like it is built to be the developer blogging platform. The code blocks, the post generation, and the fact that it is built on Ruby all helped me settle on Octopress as my new blogging platform. I hope you enjoy the new site!