Zend Framework Database Cheatsheet

Filed Under (General) by Wenbert on 30-08-2007

Tagged Under : ,

For those of you who are a lot like me - those who can’t live without cheatsheets, I have made some sort of cheatsheet for the Zend Framework Database-thingies (Zend_Db, etc.).

It has notes on how to insert a row, updating, deleting, fetching, table joins, transactions. I could never memorize anything, and this page has been very useful to me - just sharing.

Click here for the Zend_Db cheatsheet.

jQuery AJAX in Zend Framework

Filed Under (General) by Wenbert on 28-08-2007

Tagged Under : , ,

I have posted a sample code on my wiki on how to submit variables to an action controller. The sample code contains a niffty jQuery plugin - the jQuery Calendar.

Click here for the tutorial.

Update (07-08-2008): For those who don’t know, Zend Framework has built-in stuff for Dojo. So you might want to check it out first. I still prefer jQuery though.

Update (05-07-2008): Since this post has been getting a lot of traffic and the tutorial to my wiki is not that good, I have decided to update this page and post a simpler and shorter tutorial.

So here it is…

The Javascript will look something like this:

  1. //For Edit Action
  2. f u n c t i o n editnote(notes_id)
  3. {
  4. //a sample loading image thingy
  5. $(‘#notes_entry_id’).prepend("
  6. <div id="loading"><img src="http://blog.ekini.net/wp-admin/"+baseUrl+"/images/loading.gif" alt="" /></div>
  7. ");
  8. //Get notes from database - in ZF, notes = controller and getnoteforedit = action
  9. $.post(baseUrl+"/notes/getnoteforedit",{
  10. notes_id: notes_id
  11. }, function(data){
  12. $(’#loading’).remove();
  13. obj = window.eval(data);
  14. $(’#textarea_id’).val(obj[’notes’]);
  15. }, "json"); //this returns JSON.
  16. }

Then my controller would look something like this:

  1.    /**
  2. * Get note for action
  3. *
  4. */
  5. public function getnoteforeditAction()
  6. {
  7. $this->_helper->layout->disableLayout();    //disable layout
  8. $this->_helper->viewRenderer->setNoRender(); //suppress auto-rendering
  9. //the 2 lines above are very important. this action would return html tags from the layout and will look for a phtml file. we disable the layout and suppress auto-rendering of the phtml view files SO that our JSON will be echoed properly to the Javascript…
  10. require_once(‘models/Notes.php’);
  11. require_once(‘models/UsersNotes.php’);
  12. try {
  13. if (!$this->_request->isPost()) {
  14. throw new Exception(‘Invalid action. Not post.’);
  15. }
  16. $Notes = new Notes();
  17. $data = array();
  18. if ($result = $Notes->fetchRow("notes_id=".$Notes->getAdapter()->quote($_POST[‘notes_id’])."")->toArray()) {
  19. $data[‘notes_id’]             = $result["notes_id"];
  20. $data[‘notes’]                = $result["notes"];
  21. $data[‘datetime_posted’] = $result["datetime_posted"];
  22. $data[’status’]               = $result["status"];
  23. $json = Zend_Json::encode($data);    //basically, $data array will also be available in the JS.
  24. } else {
  25. throw new Exception(‘Note ID not found.’);
  26. }
  27. echo $json; //this will echo JSON to the Javascript
  28. unset($json);
  29. unset($data);
  30. } catch (Exception $e) {
  31. echo $e->getMessage();
  32. }
  33. }

That’s about it… :D
The $data that was encoded using Zend_JSON in the controller/action can now be accessed in the Javascript after you call the eval().

PHP + AJAX Security Issues

Filed Under (Uncategorized) by Wenbert on 28-08-2007

Tagged Under : , ,

I have been using PHP with jQuery so this one caught my attention.

It’s easy to get caught up in the dynamic potential of Ajax. But with innumerable possibilities also comes increased risk. If security isn’t a major concern, it should be.

Consider a registration form built out of PHP. Any aspect of your script that accepts and processes data is a potential point of attack. If you add Ajax, what you’re doing is increasing the complexity of the application and, by extension, introducing greater vulnerability. More points of entry equal a larger attack surface, and that means potential problems for your application.

Get the article here.

PHP now returns correct status code for error pages

Filed Under (Uncategorized) by Wenbert on 26-08-2007

Tagged Under :

Someone just found out that PHP now sends out php-med-trans-light1.gifthe proper HTTP headers when an error occurs on a page. Let’s say that you have a parse error on your code, the header sent will be something like 500 - instead of the 200 (which means the page is okay, thus confusing search engine robots and other scripts). This is as of PHP 5.2.4

YSlow! Tool for Web Developers from Yahoo

Filed Under (Uncategorized) by Wenbert on 17-08-2007

Tagged Under :

YSlow analyzes web pages and tells you why they’re slow based on the rules for high performance web sites. YSlow is a Firefox add-on integrated with the popular Firebug web development tool.

YSlow gives you:

  • Performance report card
  • HTTP/HTML summary
  • List of components in the page
  • Tools including JSLint

Click here for YSlow!

Five Tools for PHP Developers

Filed Under (Uncategorized) by Wenbert on 16-08-2007

Tagged Under : ,

I just found this at digg. Pretty interesting - but Zend Framework should be there :P I know it is not a tool, but who cares.

A snippet:

PHP LogoAfter working on several large scale PHP projects, and writing a lot of PHP code, I’ve discovered a number of tools that improve code quality, streamline rollouts, and generally make life as a PHP developer a whole lot easier. Many of these tools probably deserve a post of their own. But, since some people aren’t even aware that these tools exist, I figured I’d start there. So, without further ado, here’s my list of tools that every PHP programmer should know about.

Phing - a project build system

Phing is a project build system based on Apache ANT. The name is a recursive acronym, of sorts, that stands for PHing Is Not GNU make. Phing can do anything a traditional build system like GNU make can do, but without the steep learning curve.

The idea behind phing (and other build tools) is to evaluate a set of dependencies, then execute a set of PHP classes to properly install and configure an application. The build process is controlled by a simple XML configuration file. Out of the box, phing can perform token replacement (e.g., to change include paths on your development and production systems), execute SQL, move and copy files, run shell scripts, and more. You can also create your own custom tasks by extending the “task” class included with the package.

Phing is an invaluable tool for anyone who needs to deploy large scale PHP applications on more than a single server. But I’ve found it useful for simple scripts, too.
Xdebug - debugger and profiler tool

Xdebug is a PHP extension that helps you debug and profile scripts. Among the most useful features of Xdebug are the new notice, warning, and error messages that are displayed after activation. If a script fails to execute properly, Xdebug will print a full stack trace in the error message, along with function names, parameter values, source files, and line numbers. A welcome feature for developers who are tired of the skimpy error reports from a default PHP install.

The extension has a number of more advanced features that allow developers to perform code coverage analysis, collect profiling information, and debug scripts interactively. The profiling functionality is particularly useful. The profiler uses a common output file format, allowing you to use tools like KCacheGrind to quickly find bottlenecks in your code. A good profiler is an essential tool for any serious developer, as it allows you to properly optimize your code while avoiding the hazards of premature optimization.

PHPUnit - unit testing framework

PHPUnit is a lightweight testing framework for PHP. It’s a complete port of JUnit 3.8.1 for PHP5, and is a member of the xUnit family of testing frameworks (which are based on a design by software patterns pioneer Kent Beck).

Unit tests form the foundation of several modern agile development methodologies, making PHPUnit a vital tool for many large scale PHP projects. The tool can also be used to generate code coverage reports using the Xdebug extension discussed earlier, and integrates with phing to automate testing.

I am particulary interested in PHPUnit. I have used it before - about a year ago - and I can’t wait to use in with the Zend Framework.

Complete article is found here.

Zend Framework Tutorial: A different Access Control List implemented

Filed Under (Uncategorized) by Wenbert on 11-08-2007

Tagged Under : ,

I have implemented a different Access Control List using Zend Framework. It does not use Zend_Acl - I had a hard time implementing it. Too hard and too confusing :( And there are no real-world examples on how to use it. The documentation in the manual is too simple. Using it is easy - but implementing it is a different story.

This implementation uses a Plugin that will be called when a controller is accessed/loaded. The plugin is executed just before the controller’s action is “executed”. In short, it is in the preDispatch().

Please note that this tutorial is written AS IS. If you find mistakes, then feel free to correct me. Also, it only gives you the first step. I still have to write the user interface for assigning roles, white-list, and so on and so fourth. But if you get the idea, you could do that on your own.

Click here for the tutorial.

Zend Framework Tutorial: Creating your own Action Helper (fixed)

Filed Under (Uncategorized) by Wenbert on 11-08-2007

Tagged Under : ,

I have written a simple tutorial on how you can create your own Action Helper using the Modular Directory Structure.

To create your own action helper in your own namespace using the Modular Directory Structure, do the following:

1. Add this line in your index.php (bootstrap file): Zend_Controller_Action_HelperBroker::addPrefix(’Ekini_Controller_Action_Helper’);
2. Go to your library directory
3. Create a directory named: Ekini (or your own name)
4. Go inside Ekini directory
5. Create a directory named: Controller
6. Go inside the Controller directory
7. Create a directory named: Action
8. Go inside the Action directory

View the entire tutorial by clicking here. *Link is now fixed*

Connect to JD Edwards using PHP, Apache2 and unixODBC

Filed Under (Uncategorized) by Wenbert on 09-08-2007

Tagged Under : ,

A few weeks back, I was required to connect to JDE using PHP. After a few days of research, I was able to do so using unixODBC. Click here for the article.

Adding / Modifying a table row using javascript

Filed Under (General) by Wenbert on 08-08-2007

Tagged Under :

I have written a short and simple example on how you can add a row using JavaScript. The row added will have an Id, onMouseOver and onMouseOut functions, and a class name assigned into it. Click here for the article.

This other example shows you how to modify a the cell or <TD> inside each <TR> tag. It is useful when you want to modify the entire row when you have already assigned a classname to the entire <TR> tag.

Subscribe to Rss Feed : Rss