CakePHP-Fr.Org

Forum francophone du Framework CakePHP

Vous n'êtes pas identifié.

#1 10-03-2015 15:30:57

Salaktarus
Chouquette
Date d'inscription: 10-03-2015
Messages: 1

how to keep my product ID in cakephp 2.0

i have two table Location and Car , what I want is:

when I click on the picture of the car (View / Cars / view.ctp)

redirected to the location add form (View / Locations / add.ctp) while keeping the ID of the car I've previously chosen



this is 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 __('Picture'); ?></dt>
        <dd>
            <?php echo h($car['Car']['picture']); ?>
        </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>

Hors ligne

 

#2 10-03-2015 21:28:53

cake17
Cooker
Date d'inscription: 29-03-2012
Messages: 381
Site web

Re: how to keep my product ID in cakephp 2.0

Hi spartakus,

Welcome to this forum ! I have to precise here that most of the people here are french, so maybe it's better for you to go to #cakephp IRC or to english google groups to have answers to your questions.

That said, maybe if you pass your params in the link, something like this:

Code: php

  1. $this->Html->link(__('Rent a Car'), array('controller'=>'locations','action' => 'add', $carId));

Of course, you would need to change your action in your controller Locations:

Code: php

  1. Class LocationsController {
  2.     public function add($carId) {
  3.         // all your validation or/and save operations here
  4.     }
  5. }

Let me know if this works :-)

Hors ligne

 

Pied de page des forums

Propulsé par FluxBB
Traduction par FluxBB.fr