SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

  Archive for May, 2007

May 26, 2007 – 2:59am Count your SLOC: Source Lines of Code

Although this number may mean nothing at all, counting the total number of Source Lines of Code (SLOC) can be fun. Here is how you can tally up all the lines of PHP you have in a project from the command line:

find . -regex '.*\.\(php\)' -print0 | xargs -0 cat | wc -l

You can read more about this on wikipedia.

Posted by in  Web Development   |     |  1 Comment »

May 22, 2007 – 1:23pm TextMate Tip of the Day: Piping SVN diff

  • pipe svn output with | mate

You can get a great view of the changes you’ve made in a file under version control (with svn) if you pipe the command through to TextMate:

svn diff web/myfile.php | mate

You can also do this with svn cat:

svn cat -r## filename | mate

This will open up the resulting diff output in a new document in TextMate, all color coded and ready to go.

SVN Diff

Posted by in  OS X Web Development   |  Comments Off on TextMate Tip of the Day: Piping SVN diff

May 22, 2007 – 11:20am How to set up an SSH tunnel to a remote MySQL server

If you have SSH access to your hosting account and want to use a GUI to view your MySQL databases on that remote server, you can set up an SSH tunnel and use a program like CocoaMySQL to manage your remote database. When it works, it can be really userful and is a nice break from a web-based solution like phpMyAdmin.

First, make sure you can login to your host through SSH. After you are certain it works, you can open up a tunnel like this:

ssh -L 3307:[ip address of host]:3306 [username]@[ip address of host] [-p ssh portnumber]

This opens up a tunnel from your local port 3307 to port 3306 on the remote host. Here is the example again with dummy data, using a non-standard ssh port 8022:

ssh -L 3307:192.168.2.1:3306 user@192.168.2.1 -p8022

Once that connection is set up, fire up your MySQL GUI and connect to your local IP address (127.0.0.1) and specify the new port you set up in your tunnel (3307). Here is how it looks in CocoaMySQL.

CocoaMySQL Connection

Posted by in  OS X Web Development   |     |  Comments Off on How to set up an SSH tunnel to a remote MySQL server

May 16, 2007 – 2:02pm Symfony Cookbook

There are a few additional sections that didn’t make it into the Symfony book but are  available online for reference as part of the Symfony Cookbook. Although not officially released, you can browse the Symfony cookbook within trac at:

There are sections about emailing, cookies, paginating results, the command line interface, shopping cart plugin, and more.

Posted by in  Uncategorized   |     |  Comments Off on Symfony Cookbook

May 15, 2007 – 5:54pm Extending sfPager to paginate a large array

Inspired by a snippet on the symfony site, here is a version I created that inherits from sfPager. I had a collection of objects (not propel objects) that I needed to iterate through and this did the trick. Note that this is not really optimal because the full array has to be populated during each request, whereas something like sfPropelPager is smart enough to only fetch/hydrate the objects you are going to need for that particular view. Nonetheless, this does the job pretty well.

Posted by in  Web Development   |     |  1 Comment »

May 15, 2007 – 4:32pm TextMate Tip of the Day

  • Hit control-shift-v to validate PHP syntax.

At first I was skeptical about TextMate and didn’t want to fall into all the hoopla surrounding it… how are people so obsessed with their text editors to the point where they debate them to death? Most people wanted to hop on the Rails bandwagon, saw the screencast where DHH uses TextMate, and proceeded to download it. At least that’s what I did.

Now, after using it for months, there is no way I could work without it. I learn new tricks every day just by searching for features I think would be nice to have—Most often those features are already there.

The feature I just discovered today was the “Validate Syntax” for PHP documents. Usually I write some code, then go back into my browser and hit reload to view my changes, only to be stopped dead in my tracks with a syntax error. (Side note: if you are editing an HTML document you can automatically refresh current browsers from within TM by hitting apple-R.) Instead of waiting for your source to be loaded in through your browser, just hit control-shift-v to see where you left off the semicolon which you were so good at remembering until you started coding in Ruby, and continue on your merry way.

Posted by in  Web Development   |  Comments Off on TextMate Tip of the Day

May 15, 2007 – 3:17pm OS X Shortcuts, Terminal, Emacs

There are a lot of key commands that exist in OS X that are based on the standard Emacs keys (which admittedly I know nothing about). In bash, tcsh, and other unix environments you will find a lot of these work, and sometimes they will even work in Cocoa text editing tools as well.

These are supported by Mail, TextEdit, and other a lot of other Cocoa apps:

  • ctrl-a Move to the beginning of the line
  • ctrl-e Move to the end of the line
  • ctrl-f Move forward one character
  • ctrl-b Move back one character
  • ctrl-n Move to next line
  • ctrl-p Move to previous line
  • ctrl-d Delete character to right of cursor
  • ctrl-h Delete character to left of cursor
  • ctrl-k Kill from cursor to end of line (kill remembers what was deleted)
  • ctrl-y Yank back what was killed, at the cursor
  • (these two use their own clipboard, not the cmd-x/c/v one)
  • ctrl-o Insert line after cursor
  • ctrl-t Transpose characters on either side of cursor
  • ctrl-l Center the display on cursor
  • ctrl-v Scroll the display one screen forward

Here’s a pretty complete list of existing Mac key bindings for editing:

<http://www.hcs.harvard.edu/~jrus/Site/system-bindings.html>

Here’s how to add even more bindings to Cocoa (by adding them to

~/Library/KeyBindings/DefaultKeyBinding.dict):

<http://www.hcs.harvard.edu/~jrus/Site/Cocoa%20Text%20System.html>

And more on Emacs productivity and the Mac:

<http://bc.tech.coop/blog/060620.html>

Posted by in  OS X   |  Comments Off on OS X Shortcuts, Terminal, Emacs

May 14, 2007 – 2:14am javascript urlencode

In doing a google search for javascript urlencode, I realized I was using that terminology just because I was familiar with the PHP function of the same name. Javascript’s method is actually called encodeURI() which will encode all characters except:

, / ? : @ & = + $ #

But what I really wanted to do was get javascript to convert a string that would be okay to put into a query string of a URL. For this, use encodeURIComponent(). This will convert all characters except alphabetic, decimal digits, and:

- _ . ! ~ * ' ( )

That’s more like it. You can view the complete reference at mozilla or w3schools.

Posted by in  Web Development   |     |  5 Comments »

May 12, 2007 – 5:29pm Left Joins with multiple conditions using Propel Criteria

Symfony by default uses Propel for its ORM. Although Propel supports supports custom SQL I have learned how to take advantage of the built-in Criteria class to help construct queries. As described in the Propel trac,

…its database neutrality and logical simplicity make it a good choice for expressing many common queries; however, for a very complex query, it may prove more effective (and less painful) to simply use a custom SQL query to hydrate your Propel objects. (propel trac)

In building a complex search system for a client, I benefited greatly from being able to add and remove columns and conditions to a criteria object on the fly without having to worry about the actual SQL syntax. However, eventually I hit a wall when I needed to add a left join using multiple columns. Adding left joins to a criteria object is typically done like this:

$c = new Criteria();
$c->addJoin(TABLE_A::ID, TABLE_B::ID, Criteria::LEFT_JOIN);
/* equivalent to: "...left join TABLE_B ON (TABLE_A.ID=TABLE_B.ID)" */

However, this syntax does not support a join with multiple conditions, so you can’t produce something like this:

...left join TABLE_B ON (TABLE_A.ID=TABLE_B.ID AND TABLE_B.VALUE &gt; 100)

In playing around quite a bit and examining the source code, I realized you could hack the addJoin method call and trick it into adding the extra column for you. The addJoin method actually is creating a new Join object that has a left and right column definition as defined in the method call. These are used to build the join condition:

$condition = $join->getLeftColumn() . '=' . $join->getRightColumn();

(see the source)

So, if you add the extra columns to the RightColumn like so:

$c->addJoin(TABLE_A::ID, TABLE_B::ID.' AND '.TABLE_B::VALUE.' > 0', Criteria::LEFT_JOIN);

the generated SQL will yield something like this:

...LEFT JOIN TABLE_B ON (TABLE_A.ID = TABLE_B.ID AND TABLE_B.VALUE > 0)

Although it’s a hack and it may not work under all conditions, so far it has been a suitable workaround.

May 1, 2007 – 3:00pm Installing Shared Symfony Library on Remote Host with SSH

checkout symfony from svn into ~/symfony at the root of your home directory (not into public_html or httpdocs):

svn checkout http://svn.symfony-project.com/trunk ./symfony

create your new project directory

mkdir ~/mymyNewSite

create the database on the new host
Set up subdomain for new project in cPanel
set up the symfony environment settings in the following two files:
databases.yml, properties.ini
upload the symfony project into the new project directory

./symfony sync staging go

remove the default subdomain directory from public_html and create a new symbolic link to the symfony project directory

cd ~/public_html
mv
myNewSite myNewSite.bkp
ln -s /home/stereodv/
myNewSite/web myNewSite

populate the database with the data. look in data/sql/ for lib.model.schema.sql or generated-schema.sql

mysql -u myNewSite_dbuser -p myNewSite_dbname < data/sql/lib.model.schema.sql

remember to add any migrations found in data/migrate

Notes:
make sure you copy the contents of config/config.php to the server; often this file is excluded from rsync

Posted by in  Web Development   |     |  Comments Off on Installing Shared Symfony Library on Remote Host with SSH