1

I am new to cakephp and am trying to create a really simple api where the user can 'reserve' a vehicle through an api.

I have three Models, Views and Controllers, which are Vehicles, Customers and Reservations. I have implemented most of it but i am just stuck on how i can add a reservation by allowing the user to pick from a dropdown list.

Basically i want two dropdown lists that allow me to select the customer id/name and vehicle id/name. and put them into the reservations table.

This is my reservations controller add function

function add() {
        if($this->request->is('post')) {
            if($this->Reservation->save($this->data)) {
                $this->Session->setFlash('The Reservation was successfully added!');
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash('The Reservation was not added.');
            }
        }
        $customers = $this->Customers->find('list');
        $this->set(compact('customers'));
        $this->set('title_for_layout','Add Reservation');
    }

At the bottom i have used some code from a different website that should create a list of customers, but i get an error. Also because the customers content is in a different model, im guessing this is the wrong way to go about it.

So how can i add a dropdown list of customers to my reservations page.

2 Answers 2

4

Model-names should be singular, not plural, so try this

$customers = $this->Customer->find('list');

However, you may have forgotten to load the 'Customer' model in your controller. Ff the Reservation model has a relation with Customer (e.g. Reservation belongsTo Customer), it's better to access it via the Model-tree;

$customers = $this->Reservation->Customer->find('list');
$this->set(compact('customers'));

To get rid of the intermediate variable, use this:

$this->set('customers', $this->Reservation->Customer->find('list'));
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Thanks for the reply. It is now giving me this error:- 'Call to a member function find() on a non-object'
@user667430 That would mean your Reserveration belongsTo Customer association has not been set. If you associate them, the find should work.
Hi thanks for your help, I managed to fix the relationships out. It works.
-1

Make list array in controller.

$role=$this->Modelname->find('list',array('fields'=>'id,name'));

Make drop down list in ctp file like this.

echo $this->Form->input('role', array('options' => $roles, 'default' => '--Select--'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.