cakePHP版本2.4.5
我已经设法使用element和pagesController从我的数据库创建一个动态下拉菜单。
在我的元素 navigation.ctp中
下面的代码通过来自布局default.ctp
foreach ($events as $event): ?>
<li>
<a href="/cake_sports/pages/events/<?php echo $event['Event']['id'] ?>"><?php echo
$event['Event']['description']; ?></a>
</li>
<?php endforeach; ?>
<?php unset($event); ?>
在我的 PagesController display()中:
这将从数据库中获取项目并将其传递给布局default.ctp
$this->Event->unbindModel(array('hasMany'=>array('Player')));
$this->set('events', $this->Event->find('all'));
在我的布局 default.ctp 中 这将使用$ events作为参数调用navigation.ctp元素
<?php
echo $this->element('navigation', $events);
?>
问题:当我导航或转到EventsController以外的其他页面时,我会收到错误消息。错误是可以理解的,因为我知道它不会识别$this->set('events', $this->Event->find('all'));
。但是我不知道如何使它成为全局的,所以即使在用不同的控制器更改页面之后它也不会搞砸。
答案 0 :(得分:2)
您可以将其放在AppController
public $uses = array('Event');
public function beforeFilter() {
$this->Event->unbindModel(array('hasMany'=>array('Player')));
$this->set('events', $this->Event->find('all'));
parent::beforeFilter();
}
只有在所有页面上都需要时才这样做。
此外,您可以更改
<a href="/cake_sports/pages/events/<?php echo $event['Event']['id'] ?>"><?php echo $event['Event']['description']; ?></a>
与
echo $this->Html->link($event['Event']['description'], array('controller' => 'pages', 'action' => 'events', $event['Event']['id'] ));
答案 1 :(得分:1)
Controller :
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
var $helpers = array('Html', 'Form', 'Js');
public $components = array('Flash','RequestHandler');
public $uses = array('User','State','City');
public function getByCity() {
$state_id = $this->request->data['User']['state_id'];
$cities = $this->City->find('list', array(
'conditions' => array('City.state_id' => $state_id),
'recursive' => -1
));
$this->set('cities',$cities);
$this->layout = 'ajax';
}
public function index() {
if ($this->request->is('ajax')) {
$term = $this->request->query('term');
$emails = $this->User->getEmails($term);
$this->set(compact('emails'));
$this->set('_serialize', 'emails');
}
if($this->request->is('post')){
if($this->User->save($this->request->data)) {
$this->Session->setFlash('Successully save your information!!');
$this->redirect('index');
}
else{
$this->Session->setFlash('Unable to save your Information!!');
$this->redirect('index');
}
}
$states = $this->User->State->find('list');
$cities = $this->User->City->find('list');
$this->set(compact('states', 'cities'));
}
}
User Modal :
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $name = 'User';
public function getEmails ($term = null) {
if(!empty($term)) {
$emails = $this->find('list', array(
'fields' => array('User.email'),
'conditions' => array(
'email LIKE' => trim($term) . '%'
)
));
return $emails;
}
return false;
}
public $belongsTo = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id'
),
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id'
)
);
//put your code here
public $validate=array(
'name'=>array(
'rule'=>'notBlank',
'required'=>true,
'message'=>'Input your name'
),
'email'=>array(
'email'=>array(
'rule'=>'email',
'message'=>'Input a valid emial address'
),
'email'=>array(
'rule'=>'notBlank',
'required'=>true,
'message'=>'Input your email address'
)
),
);
}
?>
City Model :
<?php
App::uses('AppModel', 'Model');
class City extends AppModel {
public $name = 'City';
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'city_id'
)
);
}
?>
State Model :
<?php
App::uses('AppModel', 'Model');
class State extends AppModel {
public $name = 'State';
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'state_id'
)
);
}
?>
index.js :
(function($) {
$('#autocomplete').autocomplete({
source: "http://localhost/save/users/index.json"
});
})(jQuery);
index.ctp :
<?php
//let's load jquery libs from google
$this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('inline' => false));
$this->Html->script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array('inline' => false));
//load file for this view to work on 'autocomplete' field
$this->Html->script('View/Users/index', array('inline' => false));
?>
<?php
echo $this->Form->create('User'); ?>
<fieldset>
<legend>Add New User</legend>
<?php
echo $this->Form->input('name',array('required'=>'required'));
echo $this->Form->input('email', array(
'id' => 'autocomplete'));
echo $this->Form->input('country',array('options'=>array(
'bangladesh'=>'Bangladesh',
'india'=>'India',
'pakistan'=>'Pakistan' ,
'usa'=>'USA',
'uk'=>'UK',
'canada'=>'Canada'
)
));
echo $this->Form->input('state_id');
echo $this->Form->input('city_id');
echo $this->Form->end('Save');
?>
</fieldset>
<?php
$this->Js->get('#UserStateId')->event('change',
$this->Js->request(array(
'controller'=>'Users',
'action'=>'getByCity'
), array(
'update'=>'#UserCityId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
get_by_city.ctp :
Controller :
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
var $helpers = array('Html', 'Form', 'Js');
public $components = array('Flash','RequestHandler');
public $uses = array('User','State','City');
public function getByCity() {
$state_id = $this->request->data['User']['state_id'];
$cities = $this->City->find('list', array(
'conditions' => array('City.state_id' => $state_id),
'recursive' => -1
));
$this->set('cities',$cities);
$this->layout = 'ajax';
}
public function index() {
if ($this->request->is('ajax')) {
$term = $this->request->query('term');
$emails = $this->User->getEmails($term);
$this->set(compact('emails'));
$this->set('_serialize', 'emails');
}
if($this->request->is('post')){
if($this->User->save($this->request->data)) {
$this->Session->setFlash('Successully save your information!!');
$this->redirect('index');
}
else{
$this->Session->setFlash('Unable to save your Information!!');
$this->redirect('index');
}
}
$states = $this->User->State->find('list');
$cities = $this->User->City->find('list');
$this->set(compact('states', 'cities'));
}
}
User Modal :
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $name = 'User';
public function getEmails ($term = null) {
if(!empty($term)) {
$emails = $this->find('list', array(
'fields' => array('User.email'),
'conditions' => array(
'email LIKE' => trim($term) . '%'
)
));
return $emails;
}
return false;
}
public $belongsTo = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id'
),
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id'
)
);
//put your code here
public $validate=array(
'name'=>array(
'rule'=>'notBlank',
'required'=>true,
'message'=>'Input your name'
),
'email'=>array(
'email'=>array(
'rule'=>'email',
'message'=>'Input a valid emial address'
),
'email'=>array(
'rule'=>'notBlank',
'required'=>true,
'message'=>'Input your email address'
)
),
);
}
?>
City Model :
<?php
App::uses('AppModel', 'Model');
class City extends AppModel {
public $name = 'City';
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'city_id'
)
);
}
?>
State Model :
<?php
App::uses('AppModel', 'Model');
class State extends AppModel {
public $name = 'State';
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'state_id'
)
);
}
?>
index.js :
(function($) {
$('#autocomplete').autocomplete({
source: "http://localhost/save/users/index.json"
});
})(jQuery);
index.ctp :
<?php
//let's load jquery libs from google
$this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('inline' => false));
$this->Html->script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array('inline' => false));
//load file for this view to work on 'autocomplete' field
$this->Html->script('View/Users/index', array('inline' => false));
?>
<?php
echo $this->Form->create('User'); ?>
<fieldset>
<legend>Add New User</legend>
<?php
echo $this->Form->input('name',array('required'=>'required'));
echo $this->Form->input('email', array(
'id' => 'autocomplete'));
echo $this->Form->input('country',array('options'=>array(
'bangladesh'=>'Bangladesh',
'india'=>'India',
'pakistan'=>'Pakistan' ,
'usa'=>'USA',
'uk'=>'UK',
'canada'=>'Canada'
)
));
echo $this->Form->input('state_id');
echo $this->Form->input('city_id');
echo $this->Form->end('Save');
?>
</fieldset>
<?php
$this->Js->get('#UserStateId')->event('change',
$this->Js->request(array(
'controller'=>'Users',
'action'=>'getByCity'
), array(
'update'=>'#UserCityId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
get_by_city.ctp :
<?php foreach ($cities as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php endforeach; ?>