Read an article that I got off the Zend Developer Zone about dependency issues and how to work with them.
Archive for the 'PHP' Category
Dependency Injection and Zend Framework Controllers
Published August 8, 2008 Zend Framework 0 CommentsUsing jQuery UI with Zend Framework
Published June 25, 2008 Development , Javascript , PHP , Zend Framework , jQuery 2 CommentsjQuery is in my opinion the best javascript library. The Zend Framework guys recently decided to partner with dojo, which is ok but that isn’t my personal choice.
This doesn’t mean though that you can’t use another library, it will just require a little effort on your part. jQuery also makes a UI library that can be integrated into the Zend Framework using View Helpers.
In the Zend Framework, using Zend_Db_Table as your model class is not well advised. This practice can force you to put business logic in other places besides your model class. This may not be a big deal if your logic is simple but imagine the logic is fairly complicated. Now also imagine that the logic changes at some point. This can be a problem if you have this logic written in many different places.
There’s a solution to this problem though. You could write a Model class that uses Zend_Db_Table as a storage medium. Or, you can extend Zend_Db_Table to add your logic to the class. There is a way to simplify this process and that’s what this blog post is about.
It’s a joyous day for me because ZF 1.5 has been released.
Zend_Db_Table: Fetch a random row
Published March 12, 2008 Development , PHP , Zend Framework 0 CommentsEver wanted to fetch a random row or rows from a table with Zend_Db_Table? I know I have. And now you can very easily.
Zend_Loader::loadClass('Zend_Db_Table');
class Db_Table extends Zend_Db_Table
{
public function fetchRandom($where = null, $order = null,
$count = null, $offset = null)
{
if($order != null)
$order = 'RANDOM(), ' . $order;
else
$order = 'RANDOM()';
return $this->fetchAll($where, $order, $count, $offset);
}
}
You can use this function just like you would use fetchAll and it will return the rows in a random order, or you can limit it to 1, 5, etc. to just get some random rows. Unfortunately this code is specific to Postgres, but you can make this work with MySQL by changing RANDOM() to RAND(). For more information on getting this code to work with other databases, see this site.
This is equivalent of course to giving RANDOM() as the order for fetchAll(). This solution has the advantage of only having one place to change the code should you need to switch this to another database type. It also makes the code a little easier to follow.
I’m loving working with the Zend Framework but the class names / folder structure is starting to kill me. That’s why I’m glad to hear that namespaces will be introduced in PHP 5.3. I’m a little late on this but it’s new to me. The implementation seems pretty simple but at least it’s something. See this blog post for more information.
One pattern that was not obvious, at least to me, when I first started using the Zend Framework was that my controller classes could use inheritance just like any other class in PHP. Stupid I know but it for some reason it just didn’t come naturally. I’ll explain with code.
abstract class CommonController extends Zend_Controller_Action
{
protected $_layout;
public function preDispatch()
{
// check for login, redirect to AuthController if not
}
public function postDisptach()
{
// plug in all controllers output into a layout
}
public function commonAction()
{
// this action will be present in all controllers
// which inherit from CommonController
$this->render('common');
}
}
class FooController extends CommonController
{
$_layout = 'Foo';
}
class BarController extends CommonController
{
$_layout = 'Bar';
}
Using this code, I can now go to two pages, /foo/common and /bar/common. Both pages will produce the same output if commonAction()’s output is just some simple text, but they will be presented in different layouts. I haven’t provided any code to actually wrap the output in a layout but this is more of a concept.



