Archive

Posts Tagged ‘symfony’

Pagination in symfony without criteria

November 28, 2009 1 comment

I have created a news aggregator and needed to paginate the results. I had the ‘feed’ object having an array of ‘feeditems’ objects in my actions. So, I could not use sfPropelPager as it needs a criteria object to pull objects from the database. The solution which worked for me is presented here. Disclaimer: This is a complete rip-off from the snippets section on symfony site, modified slightly to work with the latest version of symfony and php. I will add another post to paginate in sfFeed2Plugin when I have time.

Add this class to lib/helper or anywhere in lib and include it in the file you need to use pagination.

myArrayPager.class.php

<?php
class myArrayPager extends sfPager
{
protected $resultsArray = null;

public function __construct($class = null, $maxPerPage = 10)
{
parent::__construct($class, $maxPerPage);
}

public function init()
{
$this->setNbResults(count($this->resultsArray));

if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
{
$this->setLastPage(0);
} else {
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
}
}

public function setResultArray($array)
{
$this->resultsArray = $array;
}

public function getResultArray()
{
return $this->resultsArray;
}

public function retrieveObject($offset) {
return $this->resultsArray[$offset];
}

public function getResults()
{
return array_slice($this->resultsArray, ($this->getPage() – 1) * $this->getMaxPerPage(), $this->maxPerPage);
}
}
?>

In the action, instead of setting an object holding array of items, set the pager.

$empager = new myArrayPager(null, 10); //10 items per page
$empager->setResultArray($this->feed->getItems()); //my object is ‘feed’ which has ‘items’ array
$empager->setPage($request->getParameter(‘page’, 1));
$empager->init();

$this->pager = $empager;

Implementation of view is exactly the same as with sfPropelPager or the equivalent doctrine pager.

Enjoy!

Advertisements

Trials and tribulations of setting up LAMP on Ubuntu

October 2, 2009 1 comment

I started with a quick assessment of available web frameworks/architectures (assume either, not delving into semantics). I have worked extensively in J2EE, used Hibernate for ORM along with Spring for dependency injection, reduce mundane coding…blah, blah…struts and AJAX. After scanning the “experts” views on many available web frameworks/architectures on the internet, I narrowed down to Ruby-on-Rails (RoR) and LAMP for trials.

Btw, moving over to Linux was something I had been contemplating for some time. Ubuntu seemed the obvious choice to me. I decided to give up Windows XP completely, by far, the best OS Microsoft has had to offer. Some reviewers have said Windows 7 scores over the rest but I refuse to pay $119.00 for a “few improvements” (rather fixes on Windows Vista) when Ubuntu provides all updates for free. And yes, I am prepared to wait and test Chrome OS which again, will be free.

Moving on, my assessment of RoR: It is a vast improvement over an OO language like JAVA. Coding is very intuitive, neat and most of all, cuts down heavily on extremely verbose language that is JAVA. Rails as a framework, is ok (I didn’t investigate enough to comment more on it). However, after 3 days of poking and playing with RoR, printing “Hello World” et. al., creating silly recursive loops with “blocks” (a good selling point of Ruby), I decided to move on. Reasons – 1) The documentation for RoR is limited 2) I had a feeling that code in Ruby could ultimately become complex to read and manage. 3) PHP community is vast and so is the number of resources for PHP-related stuff.

Setting up LAMP on Ubuntu has not been easy, even with all the references and help available from online community. But after 4 days, I am ready with my first full (sample) application in PHP. List of software/frameworks/tools I have used:

1) Ubuntu 9.04 Desktop edition
2) PHP Version 5.2.6
3) Apache Web Server
4) Eclipse PDT 2.1 for IDE
5) Mysql 5.1
6) Symfony 1.2.9
7) Subversion (SVN for version control)
8) Others in the process of installing 1-7

Basic considerations: Open source, free but without compromising significantly on quality.  I will point to a few online resources I have been using for the benefit of any newbie like me.

1) Things to do after installing Ubuntu
2) Basic PHP tutorial : very nicely written for beginners
2) Ubuntu documentation: first point of reference for OS problems. great community
3) Ubuntu LAMP for newbies, ApacheMySQLPHP, Installations with SSL : installation instructions
4) Propel (for database abstraction, and required by Symfony)
5) Symfony as a complete MVC framework for PHP

…will add more to the list.

I wish I could recall the numerous problems I faced reaching up to this point, mainly because I am new to Linux…was just too excited to keep moving ahead. Finally, I have a PHP application up and running 🙂

Categories: Programming Tags: , , , , , ,