Skip to content

Instantly share code, notes, and snippets.

@housni
Created February 13, 2012 18:14
Show Gist options
  • Save housni/1818766 to your computer and use it in GitHub Desktop.
Save housni/1818766 to your computer and use it in GitHub Desktop.
How do I display model a property in a view?
//controllers/AdminsController.php
<?php
class AdminsController extends \lithium\action\Controller {
public function search() {
if (form submitted) {
//prepare the url and redirect to same method
//so that we have a _GET based search URL
} else {
//this is all _GET
$admins = null;
if (args exist) {
//do search
}
return compact('admins');
}
}
}
?>
//models/Admins.php
<?php
class Admins extends \app\extensions\data\Model {
//I want to use this to display a select list in my view
private $_search_options = array(
'email' => 'Email',
'first_name' => 'First Name',
'middle_name' => 'Middle Name',
'last_name' => 'Last Name',
'active' => 'Status',
);
public function search_options() {
return $this->_search_options;
}
}
?>
//views/admins/search.html.php
/**
* If the search returns n results, $admins will have n objects.
* So, I use the first() filter.
* But, in its initial state with no _GET args or _POST vars, $admins is null.
*
* How do I handle this? It doesn't make sense to do a `Admins::first()` or something like that
* in the AdminsController::search()
*/
<?= $this->form->create(); ?>
<?= $this->security->requestToken(); ?>
<?= $this->form->select('search_term',
$admins->first()->search_options() // <--- $admins is here
); ?>
<?= $this->form->field('search', array('value' => $search_term)); ?>
<?= $this->form->submit('Search'); ?>
<?= $this->form->end(); ?>
<?= $this->html->link('Back', array('Admins::index')); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment