我有两张桌子位置和车。我想要的是,当我点击汽车的图片(View/Cars/view.ctp
)时,重定向到位置添加表单(View/Locations/add.ctp
),同时保留我之前选择的汽车的ID。
LocationsController:
<?php
App::uses('AppController', 'Controller');
class LocationsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array(
'Js',
'GoogleMap'
);
public function index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function add($car_id) {
if ($this->request->is('post')) {
$this->Location->create();
$this->set('car_id', $car_id);
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function admin_index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function admin_view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Location->create();
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function admin_edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function admin_delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}}
and this CarsController
<?php
App::uses('AppController', 'Controller');
class CarsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array('Js', 'GoogleMap');
public function admin_index() {
$this->Car->recursive = 0;
$this->set('cars', $this->Paginator->paginate());
}
public function view($id = null){
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function admin_view($id = null) {
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Car->create();
if ($this->Car->save($this->request->data)) {
$this->Session->setFlash(__('The car has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car could not be saved. Please, try again.'));
}
}
$categories = $this->Car->Category->find('list');
$subcategories = $this->Car->Subcategory->find('list');
$this->set(compact('categories', 'subcategories'));
$this->set('categories', $this->Car->Subcategory->Category->find('list'));
}
public function admin_edit($id = null) {
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Car->save($this->request->data)) {
$this->Session->setFlash(__('The car has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->request->data = $this->Car->find('first', $options);
}
$categories = $this->Car->Category->find('list');
$subcategories = $this->Car->Subcategory->find('list');
$this->set(compact('categories', 'subcategories'));
}
public function admin_delete($id = null) {
$this->Car->id = $id;
if (!$this->Car->exists()) {
throw new NotFoundException(__('Invalid car'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Car->delete()) {
$this->Session->setFlash(__('The car has been deleted.'));
} else {
$this->Session->setFlash(__('The car could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function index() {
$this->set('cars', $this->Car->find('all'));
}
}
and this is cars/view.ctp :
<div class="cars view">
<h2><?php echo __('Car'); ?></h2>
<?php
echo $this->Html->input("cars/car_id", array(
"alt" => "Cars",
'url' => array('controller' => 'locations', 'action' => 'add', 'car_id')
));
?>
<dl>
<dt><?php echo __('Id'); ?></dt>
<dd>
<?php echo h($car['Car']['id']); ?>
</dd>
<dt><?php echo __('Title'); ?></dt>
<dd>
<?php echo h($car['Car']['title']); ?>
</dd>
<dt><?php echo __('Category'); ?></dt>
<dd>
<?php echo $this->Html->link($car['Category']['name'], array('controller' => 'categories', 'action' => 'view', $car['Category']['id'])); ?>
</dd>
<dt><?php echo __('Subcategory'); ?></dt>
<dd>
<?php echo $this->Html->link($car['Subcategory']['name'], array('controller' => 'subcategories', 'action' => 'view', $car['Subcategory']['id'])); ?>
</dd>
<dt><?php echo __('Color'); ?></dt>
<dd>
<?php echo h($car['Car']['color']); ?>
</dd>
<dt><?php echo __('Serial'); ?></dt>
<dd>
<?php echo h($car['Car']['serial']); ?>
</dd>
<dt><?php echo __('Model'); ?></dt>
<dd>
<?php echo h($car['Car']['model']); ?>
</dd>
<dt><?php echo __('Price'); ?></dt>
<dd>
<?php echo h($car['Car']['price']); ?>
</dd>
</dl>
</div>
<h5><?php echo $this->Html->link(__('Rent a Car'), array('controller'=>'locations','action' => 'add')); ?></h5>
and this locations/add.ctp :
<div class="locations form">
<?php echo $this->Form->create('Location'); ?>
<fieldset>
<legend><?php echo __('Add Location'); ?></legend>
<?php
echo $this->Form->input('status');
echo $this->Form->input('departure_date');
echo $this->Form->input('expected_return_date');
echo $this->Form->input('user_id');
echo $this->Form->input('agency_id');
echo $this->Form->input('car_id');
//echo $this->$Session->read('Auth.User.username');
//echo $this->$Session->read('Auth.Car.id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Locations'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Agencies'), array('controller' => 'agencies', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Agency'), array('controller' => 'agencies', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Cars'), array('controller' => 'cars', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Car'), array('controller' => 'cars', 'action' => 'add')); ?> </li>
</ul>
</div>
答案 0 :(得分:0)
假设您使用了之前发布的问题的答案,那么您将正确设置car_id。您现在需要做的就是正确地将它添加到add.ctp中的表单中。
替换:
echo $this->Form->input('car_id');
使用
echo $this->Form->input('car_id', array('type'=>'hidden', 'value'=>$car_id));
然后,您的表单将正确保存car_id。