Nerds who love the symfony-project
30 Jun
If you haven’t heard, Symfony 1.1 is finally out. We at SymfonyNerds.com were so excited, we gathered around one desk and went through the doco together, and then we held hands and sang kumbaya…. On a serious note some awesome features of 1.1:
You can read more about the official release notes here.
Let us know about your upgrade experiences, we will be upgrading some of our apps shortly - and will share our experiences with you.
26 Jun
We have created a room in FriendFeed for the symfony-project: http://friendfeed.com/rooms/symfony-project, its a public room - feel free to join and post your Symfony links there! We thought this would a great way to share any Symfony articles you might stumble upon. Be sure to share them using the FriendFeed bookmarklet if you do!.
Not sure what FriendFeed is? check it out, or read my blog post about getting started with the FriendFeed API.
26 Jun
Exporting to excel isn’t really something that’s symfony specific. There are plenty of good PHP libraries out there that can allow you to do some pretty advanced Microsoft Excel stuff with PHP.
In this example, I’ll show you how to get a view exporting to Excel. This is a very basic example that will get you started - with no external third-party libs.
Firstly, modify your actions class to turn off any layout template , do your sql query and then change the HTTP headers to have a response type of ‘application/msexcel’:
1 2 3 4 5 6 7 8 9 10 11 | public function executeList() { //Turn off the layout $this->setLayout(false); //My query $this->accounts = AccountPeer::doSelect(new Criteria()); //Export the output in Excel $this->getResponse()->setContentType('application/msexcel'); } |
Then in your view, all you need to do is have a basic HTML non-formatted table. For example, the contents of listSuccess.php could look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <table> <thead> <tr> <th>Account</th> <th>Type</th> ... </tr> </thead> <tbody> <?php foreach ($accounts as $account): ?> <tr> <td><?php echo $account->getCompanyName() ?></td> <td><?php echo $account->getAccountType() ?></td> ... </tr> <?php endforeach; ?> </tbody> </table> |
Thats it! Simple!
If you want to do some more advanced stuff - you should get a third-party lib, but if all your after is to export one view into a single Excel workbook with PHP and Symfony - this should be all you need.
25 Jun
Symfony has an awesome feature that automatically re-populates your form for you without writing any code. All you have to do is specify it in your app/module/validate/myForm.yml file:
fillin: enabled: true
So what happens if you have multiple forms on the same page? Well all you have to do is ensure that each one of your forms has ID and a Name:
echo form_tag('mymodule/action', 'name=myForm id=myForm');
Then in your yml validation file for that form, ensure that you specify the form name:
fillin: enabled: true param: name: myForm ....
That’s it. Click here to read more about Symfony Form Validation.
24 Jun
Wow, I’m blown away with how easy it is to get some pagination going with Symfony. In my previous life, I had coded my own custom Pagination class - it would have been easily 100 lines of code. I’ve just implemented some very basic pagination with less than 20 lines of code.
I’m assuming you have created your model objects, and have a basic setup going. Im also doing this in Symfony 1.0. Lets now go ahead and create some basic pagination.
1. Edit your actions.class.php for the module you want to paginate. In my case, I want to create a listing of all companies. For this, I’ll have a executeList action within that class. Here is what it looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class companyActions extends sfActions { ... public function executeList(){ //Get a listing of all companies $this->companys = CompanyPeer::doSelect(new Criteria()); //Lets do the pagination for it, firstly define a pager for the Company object. Lets go with 10 results per page $pager = new sfPropelPager('Company', 10); //Define the criteria, in this case, we won't have any - lets get all companies $pager->setCriteria(new Criteria()); //Set the first page to 1 $pager->setPage($this->getRequestParameter('page', 1)); $pager->init(); $this->pager = $pager; } ... }//class |
2. Now lets modify your view for listSuccess.php to display what results you are viewing
1 2 | //Display a summary of the total companies found, and how many companies there are echo $pager->getNbResults()." Companies found. Displaying results ".$pager->getFirstIndice()." to ".$pager->getLastIndice(); |
3. Now lets put in our main code to loop through the paginated results (still in listSucess.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | foreach ($pager->getResults() as $company){ echo $company->getCompanyName(); //go through and display all you want maybe in a nice table here //... } //Now lets display a small navigation bar with arrows for next, previous and page numbers echo "<br/>"; <?php if ($pager->haveToPaginate()): ?> <?php echo link_to('«', 'company/list?page='.$pager->getFirstPage()) ?> <?php echo link_to('<', 'company/list?page='.$pager->getPreviousPage()) ?> <?php $links = $pager->getLinks(); foreach ($links as $page): ?> <?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'company/list?page='.$page) ?> <?php if ($page != $pager->getCurrentMaxLink()): ?> - <?php endif ?> <?php endforeach ?> <?php echo link_to('>', 'company/list?page='.$pager->getNextPage()) ?> <?php echo link_to('»', 'company/list?page='.$pager->getLastPage()) ?> <?php endif ?> |
That’s it! Now have a play with that, and if you are gaim, you could even go and do some Ajax Pagination or get some sorting going.
24 Jun
One of the great features of symfony is of course that you can easily make a change to your model by simply updating your schema.yml file then doing a propel-build-model
The really nice part is that all the extensions you put into your model, if writen to the model extension files in /lib/model will still be there, and only the base classes in /lib/model/om are updated.
But everyone knows this, so what is the tip!? Well, if you completely remove a table from your DB, and hence from your model, you will need to manually delete the files in both /lib/model and /lib/model/om. It seems propel will add new files, modify existing, but not delete those removed from your model. And that, is your symfony tip for today.
20 Jun
This isn’t really Symfony related, but its PHP related. Its such a great feeling to know you have saved a potential 4 lines of code, by using a one-line if.
My original code:
if (sizeof($accounts==1)){ echo "There is one account attached to this company"; } else{ echo "There is ".sizeof($accounts)." accounts attached to this company"; }
That’s a massive 6 lines of code. People - think of the environment! In this day an age we can’t afford all these extra lines!
Switch it over to a one-line if. My one-liner:
//My sexy one-line if command echo "There ". ((sizeof($accounts)==1) ? " is one account attached to this company." : " are ".sizeof($accounts)." accounts attached to this company.");
Now how sexy is that! Of course, the code has become more illegible, and I’ve wasted one line commenting on how sexy it is, but heck - its cool.
17 Jun
I don’t like the default symfony ‘down arrow’ to associated with a form error. So I want to make it look a bit more sexy. Here is what you need to do:
1. Edit your application settings.yml file to overwrite the default ‘down’ arrow. The following settings indicate no prefix or suffix, but a CSS class ‘formerror’ which we will define later:
all: .settings: #Form validator style settings validation_error_class: formerror validation_error_prefix: '' validation_error_suffix: '' validation_error_id_prefix: error_for_
2. Find a nice icon to use to display next to a field with an error. For example, I like this one:
![]()
3. Update your CSS file to include a formerror class:
.formerror{ padding: 3px 20px 0px; background: url(../images/icon_sml_alert.png) no-repeat left bottom; color: red; }
4. Now when you use the Symfony form helper to validate a field, it will automatically set the CSS class to ‘formerror’. For example;
echo form_error('request_type');
Will print (if the error is triggered)
<div style="" class="formerror" id="error_for_request_type">Please specify the request type </div>
So you get something looking a bit nicer than that ‘down’ arrorw:

Looks hot! - I know you like it, now wipe off that drool on your keyboard and get back to work….
15 Jun
So we have just gone live!
./symfony init-project symfonynerds.com
Symfonynerds.com is a blog created by developers who work on the Symfony PHP Framework. We love PHP and are excited about the Symfony framework. Here we will be blogging about our learnings, tips & tricks and tutorials for the Symfony framework. We also be posting some author bio’s shortly.
Stay tuned…