Friday, May 17, 2013

Propel: Create and Retrieve

Propel (ORM) provides a way to save an object and retrieve it immediately using primary key. Lets say you have a student table with id as auto increment primary key. If you are saving a new student row, you wont specify the 'id' for the new student while saving. If you want to know the id of recently inserted row, you can simply use $student->getId() on the $student object which is used to insert the object.

Here is a code snippet to make things clear:
$student = new Student();
$student->setName("foo");
.....
.....

$student->save();                // This saves the new student
$id = $student->getId();      // This retrieves the id of recently inserted student row
$newStudent = StudentPeer::retrieveByPK($id); // This retrieves latest student object using id.


While doing this, make sure that in schema file, you specify autoIncrement = true for the id column.

Tuesday, April 30, 2013

Error: "rsync pop_dir “/home/user_x” failed: permission denied"


Recently, my code started failing in rsync stage w/o giving much information. When I tried running rsync manually, I got below error:
rsync pop_dir “/home/gaurav” failed: permission denied

I was running rsync with some one else's account with 'sudo -u otheruser' as prefix to rsync command. That user had perfect set of permissions to read from source and write to destination. And command was not cribbing about that user's permission, but was cribbing about my permissions.

After googling for some time, I discovered that its related to my permission on destination. I was running the command from /home/gaurav; and since I did not have permissions on destination, it was cribbing. 

To fix the issue, I just changed my current working directory and ran the rsync. It succeeded! This was a weird error related to the place where I'm running command from.

Wednesday, March 13, 2013

Awesome JSON Validator

Today, I was trying to decode the JSON string in PHP, which was produced by external code. json_decode was simply returning null without any more information. I tried changing the input string in many ways, but that did not help.

I went to search if there exists a good JSON validator, which will tell me the exact error with my JSON string. And here it is, the awesome JSON validator:

http://jsonlint.com/

This definitely saved a lot of time for me. Hope it helps others as well.

Friday, January 25, 2013

Fix "ereg is deprecated" errors in PHP 5.3


If you upgrade to PHP 5.3 from earlier versions, chances are that you’re going to run into a few warnings or deprecated function messages.
An example is the ereg family of functions; they are deprecated because they were slower and are less familir with alternative Perl-compatible preg family. This post helps to get rid of deprecated function warnings around ereg by replacing them with similar but more powerful preg_match functions.

To migrate ereg():

ereg('\.([^\.]*$)', $this->ile_src_name, $extension);

becomes

preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);

Notice that I wrapped the pattern (\.([^\.]*$)) around / /, which are RegExp delimiters. If you find yourself escaping / too much (for an URL for example), you might want to use the # delimiter instead.
To migrate ereg_replace():

$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);


becomes

$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);


Again, I just added delimiters to the pattern.
If you are using eregi functions (which are the case-insensitive version of ereg), you’ll notice there’re no equivalent pregi functions. This is because this functionality is handled by RegExp modifiers.
Basically, to make the pattern match characters in a case-insensitive way, append iafter the delimiter:

eregi('\.([^\.]*$)', $this->file_src_name, $extension);


becomes

preg_match('/\.([^\.]*$)/i', $this->file_src_name, $extension);

Wednesday, January 9, 2013

Adding PDT in Eclipse


For PHP development in Eclipse, one needs to add PDT (PHP Development Tools) SDK to Eclipse.
To install PDT on Eclipse, follow the steps:

1. Go to Help->Install New Software
2. Type php in search box
3. Choose "--All Available Sites--" from Work With drop down.
4. Wait for it to populate the list
5. Click on programming languages -> select PHP Development Tools SDK and follow the steps.

Sunday, November 25, 2012

Readable MySQL query output

I'm in love with MySQL. The only thing about MySQL that irritates me is its output formatting - it uses defined column width to show the content (instead of actual maximum length of the column data). As a result, "name varchar(100)" column would take 100 chars space in the output even if there are no actual names with more than say 10 characters.

As a workaround, I used to output the format into local file before going through it if there were hundreds of records in the query output. The local file would have tab delimited fields which can be imported beautifully into excel sheet.

I came across this simple setting which would allow me to see the query output in proper tabular format: 
There are 2 ways to set this, 

1. Session setting - done via MySQL prompt, and applies to current session only.
mysql> \P less -S
PAGER set to 'less -S'

2. Permanent setting - done via config file, and applies to all sessions.
In ~/.my.cnf file, add following line:
pager = less -S

This outputs the query output in a less-like format. It shows all the columns of a row in the same line. So if row length is more than the screen size, the extra data from the row goes unreachable/invisible. I select only the necessary columns so that query output fits in screen size.

Now query output looks the way I want. Lifes good!









Thursday, October 4, 2012

MySQL Explain explained

I came across this useful presentation on the MySQL EXPLAIN, so thought to embed it here.

                          
              

Follow up reading:
1. How MySQL uses indexes
2. ORDER BY optimization

Hope this is useful.
Happy optimizing!

Wednesday, August 29, 2012

Redirecting MySql query output in file

There are scenarios where one needs to copy mysql query output in a file, so that the query results can be used to mail/process.
MySql provides a nice way to do the same:

[Select Query] INTO OUTFILE [Destination File]
e.g. Select name, age from students where id between 1 and 100 INTO OUTFILE /tmp/students

This produces comma separated text file with every record on new line.
This creates a file on given location on the machine which hosts DB. There is no way to specify remote machine, so that file is saved on remote machine instead of DB server.

There are other scenarios where DB is hosted on server and user does not have log in permission for the server. In such case, one can use mysql command with -e option.
mysql -e [command] executes the mysql command, prints the output and exits. Thus, one can use following command to execute mysql command on remote host [Host] and save results in [Destination File] on local machine

mysql  -h[Host] --user=[Username] --password=[Password] -e "[Select Query] >> [Destination File]

Hope this is useful.
Happy querying!



Tuesday, May 15, 2012

Common issues faced during GWT development

Here I will keep writing general issues faced while developing a GWT app and the root causes and fixes for those issues.

1. com.google.gwt.user.client.rpc.statusCodeException the call failed on the server; see server log for details There are no information in the server logs.
Solution: The DataClass that is used to transfer data between client and server does not implement IsSerializable; make it implement IsSerializable.