Skip to main content

Posts

Passing PHP data to Apache access logs

Recently, I worked on a project to collect performance data from an application at work and pass that data to Apache access logs as part of a performance improvement project. As it turns out, PHP has a built-in function called apache_note() that your can use for this purpose. For example, if you wish to pass memory usage information to Apache access logs, add the following code to at the end of your front controller file: if (php_sapi_name() != 'cli') { apache_note('PHPMemoryUsage', memory_get_peak_usage(true)); } Then, update your Apache LogFormat directive to include this new piece of information: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{PHPMemoryUsage}n" combined

Mapping embedded documents in Doctrine MongoDB ODM

If you have a MongoDB document that has another document embedded in it, you still have to create a separate XML mapping document for the embedded one. So if you have a parent document called Activity that has an embedded document called Coordinates , the configuration would look something like this: src/Acme/ActivityBundle/Resources/config/doctrine/Activity.mongodb.xml <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd"> <document name="Acme\ActivityBundle\Document\Activity" db="acme" collection="activity" customId="true"> <field fieldName="id" id="true" strategy="INCREMENT...

Creating a Symfony2 helper

While working on a Symfony2 project yesterday, I realized I needed a better way to format dates in my templates. The reason behind this was the fact that my MongoDB document entities contained date values that were either null or DateTime instances. This entailed using a bunch of if statements for extra checks that made template code unnecessarily verbose. My solution was to create a template helper in an application specific framework bundle that I had created previously to contain all common assets and other necessary common resources. Create a Helper directory in the framework bundle directory and create a DateHelper class: src/Acme/FrameworkBundle/Helper/DateHelper.php namespace Acme\FrameworkBundle\Helper; use Symfony\Component\Templating\Helper\Helper; class DateHelper extends Helper { protected $options; public function __construct(array $options) { $this->options = $options; } public function getName() { return ...

Adding post-login logic to FOSUserBundle

Having finally figured out how to use FOSUserBundle in my project, I decided to keep track of all logins next. The implementation turned out to be a breeze thanks to Symfony2's security listener mechanism. As usual, the first step is to create a MongoDB document for this purpose. This is a very simple document that contains a user's id, session id, IP address, and login date. src/Acme/UserBundle/Document/LoginHistory.php namespace Acme\UserBundle\Document; class LoginHistory { protected $id; protected $userId; protected $sessionId; protected $ip; protected $createdAt; /** * Get id * * @return custom_id $id */ public function getId() { return $this->id; } /** * Set userId * * @param int $userId */ public function setUserId($userId) { $this->userId = $userId; } /** * Get userId * * @return int $userId */ public function getUserId...

Symfony2 choice list type for MongoDB backed forms

A couple of days ago, I had to implement a select field representing a one-to-one MongoDB relationship in one of my Symfony2 forms. Having spent some time reading the documentation, I decided to use the entity form field type for this purpose. class AcmeFormType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('parent', 'entity', array( 'class' => 'Acme\MyBundle\Document\MyDocument', 'property' => 'name', 'label' => 'Parent', 'empty_value' => 'Root' )); } } And I got presented with this exception: Class Acme\MyBundle\Document\MyDocument is not a valid entity or mapped super class. I realized Symfony2 was trying to load the necessary class using the default entity manager defined by the Doctrine ORM and failing utterly. Afte...

Integrating Memcached with Symfony2

Overview This is a short tutorial to add Memcached support to your Symfony 2 application. This is actually not a really good way to add caching support to your application as your API should support multiple caching mechanisms just like Zend_Cache does; however, all I need for my application is a way to access a Memcached singleton instance and this setup satisfies that requirement. Configuration Generate a new bundle in your application source directory using the command below. php app/console generate:bundle In your app/config.yml, create a section for your bundle: app/config/config.yml acme_memcached: servers: server1: host: localhost port: 11211 weight: 25 server2: host: localhost port: 11211 weight: 75 Note: You should also add a persistence flag to this configuration; however, I am going to omit this flag to keep this tutorial simple. Once we have a format that we like, we need t...