Call to a member function format() on a non-object Symfony datetime error

When you assign time for datetime in you Entity in Symfony project, you might get this problem. The fix is simple.
If you have your entity column setup as Date or datetime like

@ORMColumn(name="date_created", type="datetime", nullable=false)

The respective setDateCreated() method expects standard datetime object.
So,

$entity->setDateCreated(new DateTime());

will solve the problem. If you want to give different time other than today’s date, then you can populate DateTime object and provide that

Updating Git on Mac showing previous version

Was trying to pull something to github and noticed I was using a bit old Git version.
Then I went to and downloaded for mac.
After installing, I checked if I have the latest version by doing

git --version

and it showed

git version 1.7.4.4

Then I suspected the previous one is not updated and checked which git binary is being used

which git

And it replied as

/usr/bin/git

BINGO!
The new one is being save on another directory – /usr/local/git

Then I checked how my path is setup

echo $PATH

it was something like

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin..

As you can see the poor mac would see the /usr/bin first to check if command binary is inside it and it would continue.. so it was not checking the /usr/local/git..

Just putting the proper path infront of the path would solve the problem

export PATH=/usr/local/git/bin/:/usr/local/sbin/:$PATH

That would fix the problem. In the mean time, doing some cleanup on the existing git files would help also..

Unserialize giving errors at offset in php

Serialization in PHP is as easy as using serialize function. But to use the serizalized one, we need unserialize(). When you use this if you get an annoying notice of error at offset or something do this

$unserialized_value=@unserialize($serialized_value);
if ($unserialized_value)
{
    //enjoy the unserialized value here.
}

Mind you it won’t be a road blocker after all it is just a notice – but.. just cleaner is better

Country City Longitude Latitude Database setup in php

Ok, a nice Friday night here and I am working on a project that highly involves the geospatial data. For that I needed data comprising the info I needed.
Thankfully this website provided me the data as one big text file.

Then I wrote this simple snippet for porting the data to my kitchen database 😉
The script should be used only for temporary purpose and by no means on production. It doesn’t do any sanitation or what so ever the good programming.. but it does the job!
I created country_city table with the desired columns only;

CREATE TABLE `country_city` (
  `country_city_id` int(11) NOT NULL AUTO_INCREMENT,
  `country_code` char(2) NOT NULL,
  `city_name` varchar(100) DEFAULT NULL,
  `region` varchar(100) DEFAULT NULL,
  `latitude` float DEFAULT NULL,
  `longitude` float DEFAULT NULL,
  PRIMARY KEY (`country_city_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3170001 DEFAULT CHARSET=latin1

Then I run the following code to populate the data.

   <?php
   /**
    * @author Kaleb Woldearegay 
    * Code to be used "as is". Not responsible for any consequences after using the code.
    */
   const ROWS_TO_INSERT = 5000;
   $file_handle="/path/to/worldcitiespop.txt";
   $fh=fopen($file_handle, 'r');
   $query_pool=array();
   $db_handler=mysql_connect("localhost", "root", "");
   mysql_select_db("your database here");
   //assumed the database is connected at this moment..
   while(!feof($fh))
  {
      $line=fgets($fh, 1024);
      $data=explode(',', $line);
      $city=mysql_real_escape_string($data[1]);
      $query_pool[] = "(null, '{$data[0]}', '{$city}', '{$data[3]}', {$data[5]}, {$data[6]})";
      if (count($query_pool)>=ROWS_TO_INSERT)
      {
          $query = "INSERT INTO country_city VALUES " . implode(',', $query_pool);
          if (!mysql_query($query, $db_handler)) die('Error: '. mysql_error($db_handler));
          $query_pool=array();
      }
  }

That is it,
Enjoy!

symfony production path not working but works on dev environment

If you are working on the symfony and testing the changes, and you want to look at how it looks on the production env by just changing the url.. and if you are not seeing your current changes – most probably it is caching issue:
do the following being in your symfony project..

php app/console cache:clear --env=prod --no-debug

That is it

Process Commands in Linux

To stop the current process and get back to the shell:

ctrl+z

To put the current process in the backgroup

bg

To bring the background process to front

fg

To send the the job to background on one line, put ambersand (&) at the end eg usingvi command:

vi somefile.txt &

To see all the background jobjs

jobs

Bringing specific job from background to foreground

fg %1 - this will bring the job number 1 to front

List all the running processes

ps aux

To filter some process, like to get the process id and stuff

ps aux | grep "process name"

flush memcache contents from command line

We all love caching right? We have a bunch of ’em these days: query cache, file cache, page cache..
Ok, memcache.. what if you quickly wanted to flush all the contents of it on the server, just use this:

echo "flush_all" | nc  

An example for local memcache could be:

echo "flush_all" | nc localhost 11211

you would see “OK” on the command prompt after successful clearing of the cache.
happy memcaching! 🙂

The dist file “app/config/parameters.yml.dist” does not exist. Check your dist-file config or create it.

Are you upgrading symfony to 2.3. Bum! This will be the problem you would face after upgrading the composer.json file and trying to do 

composer.phar install 

the solution is simple. Just copy the existing parameters.yml file from app/config/ folder to new file called parameters.yml.dist and rerun the command