Zend Db Select with like

Like keyword can be used with Zend_Db select query


$query = $this->select()
->where("firstname like ?", $letter."%");


try that works.

There is a way to select comma separated data from any mysql table.


SELECT poll_id,group_concat(option_id) FROM `poll_elements`
group by poll_id


this will output as follows:


poll_id | group_concat(option_id)
1 | 3855,5098,8474
3 | 3855,9469,15677
6 | 509,3855,9469,10489

If the hidden field added to the Zend form leaves extra space between elements, remove those tags around it.



$this->addElement("hidden", "id",
array('disableLoadDefaultDecorators' => true));



That will do it.

This is the way to select records from tables of different databases



SELECT * FROM `db`.table tb
INNER JOIN `db2`.table2 tb2 ON tb2.id = tb.id



hope it helps someone.

Custom error messages can be added to a form element:

The following shows how to add custom error message to a select element


$countryList = array("0" => "select" ,"1" => "Canada", "2" => "India", "3" => "America");
$countries = new Zend_Form_Element_Select('country');
$countries->setLabel("Country")
->setRequired(true)
->addFilter('Int')
->addMultiOptions($countryList)
->addValidator('GreaterThan',false, array("min"=>1, "messages" => array("notGreaterThan"=>"country is required")));


validator name = GreaterThan
message for = notGreaterThan ( get it from validator library file or api doc)

The customr error message will display now.

Merge two arrays together using array_merge function



$result = array_merge(array(0=>"Hello"), array(3=>"World"));



The result will have an array with keys got re-numbered
therefore the key 3 will be changed to 1.

If you want to keep your array keys unchanged




$result = $arr1 + $arr2;


That will do it.

A text element of a form can be made read only therefore user cannot touch the value



$this->addElement('text','current_date',array(
'attribs' => array('readonly' => 'true')));


that will do it.

If you happened to select records in key value pair, try this way



$query = $this->select()
->from($this->_name, array('id', 'name'));

$this->getAdapter()->fetchPairs($query);



that will return
222 => "Velvom"

MySQL Update on Same Table

If any chance to do an UPDATE on the same table, here is the SQL for that



UPDATE my_list AS a
INNER JOIN my_list AS b
ON a.id = b.id
SET a.list_name_val = md5( b.list_name )



the above SQL will update list_name_val column with md5 of list_name.



Some Links (off programming)