如何保持ID模型在另一个Model页面在Cakephp 2 [英] How to keep ID Model in another Model page in Cakephp 2

查看:147
本文介绍了如何保持ID模型在另一个Model页面在Cakephp 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表位置和汽车。我想要的是,当我点击车的图片( View / Cars / view.ctp ),重定向到位置添加表单(

I have two tables Location and Car. What I want is, when I click on the picture of the car (View/Cars/view.ctp), redirect to the location add form (View/Locations/add.ctp) while keeping the ID of the car I've previously chosen.

LocationsController:

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>




推荐答案

对您发布的早期问题的答案将正确设置car_id。所有你现在需要做的是将它添加到您的表单中add.ctp正确。

Assuming you used the answer to the earlier question you posted you will have the car_id set correctly. All you now need to do is add it to your form in add.ctp correctly.

替换:

echo $this->Form->input('car_id');

使用

echo $this->Form->input('car_id', array('type'=>'hidden', 'value'=>$car_id));

然后,您的表单将正确保存car_id。

Your form will then save the car_id correctly.

这篇关于如何保持ID模型在另一个Model页面在Cakephp 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆