Archive | October, 2008

Logging information in database using Zend Log

31 Oct

Recently I came across a new requirement.
“Store user specific information in the database using Zend Log component”.
Although Zend has done tremendous job by creating lots of components, however documentation doesn’t have appropriate examples, so beginner and novice face lot of difficulties in using those components in the beginning.
Although I’d used Zend Log component before-for storing log information in text file, however I hadn’t used it for storing log information in the database.
Studying online manual doesn’t help me lot. Although I got basic idea from the manual, however I was unable to map information to my table- table fields were already defined.
After googling for about some time I got the solution, but example hadn’t cover how to map log information to database, instead explained how to map log data to the xml file.
However I got help from that article and wrote my own code.
I think it’ll help you out.

$db=Zend_Registry::get(‘db’);
$columnMapping = array(
‘table_column_1 =>’map1’,
‘ table_column_1’=>’map2’,
‘ table_column_1’=>’map3’,
‘ table_column_1’ => ‘map4’
);

$writer = new Zend_Log_Writer_Db($db, ‘log_actions’, $columnMapping);
$logger = new Zend_Log($writer);
$logger->setEventItem(‘ map1’, ‘ column 1 info’);
$logger->setEventItem(map1, ‘ column 2 info’);
$logger->setEventItem(‘ map1’, ‘ column 3 info’);
$logger->setEventItem(map4′, ‘ column 4 info’);

First we get instance to our $db. I assume that you have made necessary configuration in your boot file and story reference to “db” using Zend_Registry.
Next we define our “columnMapping” array. Here we are mapping our database table columns (table_column_1, table_column_2…..) to the mapping variables- these variables are used in setEventItem() method later in our code.
In the next line we have defined our writer. Zend has different writers for Db, Xml and file-streams.
As we are logging information in database so we are using Zend_Log_Writer_Db. Give database adapter, table name and columnMapping array as arguments.
Now create Zend_Log instance and give it writer instance as parameter.
The final thing is to store information in the database through mapping fields using setEventItem method.
That it.

Advertisements

Assigning multi options to dropdown in zend framework

31 Oct

After writing some post on my blog I got a question from a developer. “how to assign multioption to <select> tag?”. The answer is quite simple

Add the following code to your controller.

$form=new Zend_Form();

$dropdown= new Zend_Form_Element_Select(“dropdown”);

$dropdown->setLabel(“Country:”);

$dropdown->setMultiOptions(array(

“United States”,

“United Kingdom”,

“Canada”,

“Australia”

));

$form->addElement($dropdown);

The code above will create the following dropdown.

Zend Framework Form dropdown

Zend Framework Form dropdown

The code is self explanatory. First we are creating Zend_Form instance. Then create select element, set its label and setMultiOptions. The method setMultiOptions() take array of values.

Better to pass the following array.

array(

“1” => “United States”,

“2” => “United Kingdom”,

“3” => “Canada”,

“4” =>”Australia”

)

If you pass the above array, select element will create following html

<select name=”dropdown” id=”dropdown”>

<option value=”1”>United States</option>

<option value=”2”>United Kingdom</option>

<option value=”3”>Canada</option>

<option value=”4”>Australia</option>

</select>

Zend Dojo Form

31 Oct

As I have already discussed Dojo integration with Zend Framework in my previous post- Zend Framework and Dojo. In this post I going to show you the power of Dojo in zend. You can create a very fancy forms using Zend and Dojo. Just see the following figure.

Dojo Form

Dojo Form

You can see we have created above form with DateTextBox, CheckBox, NumberSpinner and HorizonalSlider. These are looking great and may be quite helpful in your applications.

To create above form in Zend you will need to make some necessary configuration. See my post https://zendguru.wordpress.com/2008/10/27/zend-framework-and-dojo/ for the configuration of Dojo with zend. Once you finish your configuration, add following code to your application

<?php
class DojoForm extends Zend_Dojo_Form
{
public function init()
{

$this->setAction(‘your/action/page/path’);

$this->setMethod(‘post’);
$this->addElement(

‘DateTextBox’,
‘creationdate’,

array(
‘label’          => ‘Date:’,
‘required’       => true,
‘invalidMessage’ => ‘Invalid date specified.’,
‘formatLength’   => ‘long’,

)
);
$this->addElement(

‘ComboBox’,
‘box’,
array(

‘label’ => ‘Gender’,
‘checkedValue’=>’0’,
‘unCheckedValue’=>’1’,
‘checked’ => true,
‘multiOptions’ => array(
‘0’ => ‘Male’,
‘1’ => ‘Female’

)
));
$this->addElement(

‘NumberSpinner’,
‘spinner’,
array(

‘label’ => ‘Age:’,
‘value’ => 2,
‘min’    => -10,
‘max’    => 10,
‘places’ => 2),
array(
)

);
$this->addElement(
‘HorizontalSlider’,
‘horizontal’,

array(
‘label’                     => ‘HorizontalSlider’,
‘value’                     => 5,
‘minimum’                   => -10,
‘maximum’                   => 10,
‘discreteValues’            => 11,
‘intermediateChanges’       => true,
‘showButtons’               => true,
‘topDecorationDijit’        => ‘HorizontalRuleLabels’,
‘topDecorationContainer’    => ‘topContainer’,
‘topDecorationLabels’       => array(

‘ ‘,
‘20%’,
‘40%’,
‘60%’,
‘80%’,
‘ ‘,
),

‘topDecorationParams’      => array(

‘container’ => array(

‘style’ => ‘height:1.2em; font-size=75%;color:gray;’,

),

‘list’ => array(
‘style’ => ‘height:1em; font-size=75%;color:gray;’,
),

),
‘bottomDecorationDijit’     => ‘HorizontalRule’,
‘bottomDecorationContainer’ => ‘bottomContainer’,
‘bottomDecorationLabels’    => array(

‘0%’,
‘50%’,
‘100%’,

),
‘bottomDecorationParams’   => array(

‘list’ => array(
‘style’ => ‘height:1em; font-size=75%;color:gray;’,
),
),

)
);

}
}
?>

First we extend our form from Zend_Dojo_Form instead of simple Zend_Form. We then override init method of Zend_Dojo_Form. In the next few lines we are setting form action and method attributes. These are self explanatory.

The next lines are important, because we are defining and adding dojo form elements. These line are also self explanatory. However to beginner and novice I would suggest reading my article on Zend Forms.

Zend Framework and Dojo

27 Oct

“Sorry this blog is deprecated”

“Read this article here .” http://zendgeek.blogspot.com/2009/06/zend-framework-and-dojo-configuration.html

Latest version of Zend framework- ZF 1.6.0 ship dojo toolkit. You can find it in ZendFramework-1.6.0/external/ directory when you download ZF.

Recently I come across a problem using third party “calendar”- Java script Calendar, and as Zend has now done collaboration with Dojo, so people using zend will definitely start using Dojo for java scripts and all other functionality Dojo is providing. And that’s why I decided to study and use Dojo and implement it in my application instead of fixing the issue in the third party calendar.

Although Zend has done excellent job and has made things quite easy for those who wish to use dojo in their applications, however novice and those with little experience may find it a bit difficult to configure Zend for Dojo.

It this post I will tell you how to configure Zend for working with Dojo.

The first and most important thing is to copy “external/dojo” into your js directory under www/public folder.

After you copy the dojo folder add the following code in your bootstrap.

Ignore the first line $view= new Zend_View() if you have already instantiated view object in your bootstrap file.

<body class=”tundra”>. Thats it you will now have a page like the following

Zend Form Decorators

23 Oct

Sorry this blog is deprecated.

“You can read This article here”. http://zendgeek.blogspot.com/2009/07/zend-form-decorators.html