zend framework 2 tips
************** basePath in controller
* change form element validators list:
* change elements for validation:$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');$basePath = $renderer->basePath();
* change form element validators list:
$form = new YourForm();
$form->setData ( $yourPostedData () );
$formInputFilter = $form->getInputFilter ();
// change filter
$formInputFilter->remove ( 'your_field' );
$inputFactory = new \Zend\InputFilter\Factory();
$formInputFilter->add ( $inputFactory->createInput ( array (
'name' => 'your_field',
'required' => false
) ) );
if ($form->isValid ()) {
//...
}
$form->remove( 'name' );
$form->setValidationGroup( 'date', 'text', 'public_to_group', 'public_for_everybody' );
form bind:
$form->setHydrator( new \Zend\Stdlib\Hydrator\ObjectProperty() );$form->bind( $bookletUser );
* get locale
controller, e.g.:$locale = $this->request->getQuery('language'); $this->layout()->locale = $locale;
orreturn new ViewModel(array('locale' => $locale));
Edit if you just want to get the locale from the translator, you can try this in view script:$this->plugin('translate')->getTranslator()->getLocale();
locale from http headers: http://stackoverflow.com/questions/20088050/get-local-value-in-layout-or-view-in-zend-framework-2
$dateFormat = $this->getServiceLocator()->get( 'ViewHelperManager' )->get( 'dateFormat' );$locale = $this->getServiceLocator()->get( 'ViewHelperManager' )->get( 'translate' )->getTranslator()->getLocale();
* get view helper in controller
$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager'); $escapeHtml = $viewHelperManager->get('escapeHtml'); // $escapeHtml can be called as function because of its __invoke method $escapedVal = $escapeHtml('string');
nested( in parentheses) OR or AND http://stackoverflow.com/questions/17242185/zend-framework-2-sql-select-with-or-and-and
attributes: readOnly => true
query variants:
* cool variant:https://samsonasik.wordpress.com/2014/03/19/zend-framework-2-using-insert-into-select-with-zenddb/
* (src: http://stackoverflow.com/questions/14107346/zend-framework-2-db-adapter-adapter-query-resultset-like-zf1 )
$sql = new \Zend\Db\Sql\Sql($this->tableGateway->getAdapter());
$select = $sql->select();
$select->from('table');
$select->columns(array('*'));
$select->join("join table", "table.id = join table.id", array("*"), "left");
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
return iterator_to_array($results));
$statement = $db->query($sql);
/** @var $results Zend\Db\ResultSet\ResultSet */
$results = $statement->execute();
$data = $result->getResource()->fetchAll();
// Now data is an array
$adapter = new Zend\Db\Adapter\Adapter($db_config,null,new Zend\Db\ResultSet\ResultSet('array'));
$re = $adapter->query('select * from mooncake', $adapter::QUERY_MODE_EXECUTE);
$s = $re->current();
var_dump($s);
$sql = 'select * from customer
where cust_nbr > ? and cust_nbr < ?';
$sql_result = $db->createStatement($sql, array(125000, 125200))->execute();
if($sql_result->count() > 0){
$results = new ResultSet();
$this->view->data = $results->initialize($sql_result)->toArray();
}
$lastAgenda = $a->query( $sql->buildSqlString( $s ), $a::QUERY_MODE_EXECUTE ) ->toArray();
OR
$rowSet = $sql->prepareStatementForSqlObject( $s ) ->execute();
useful things;
* \Zend\Debug\Debug::dump( $var );
Комментарии
Отправить комментарий