Question

After filtering some data, i created a variable $customers. This variable is a simple array which has the following values:

    array(
        (int) 1 => 'Customer1',
        (int) 2 => 'Customer2',
        (int) 3 => 'Customer3'
    )

I passed this array from the controller to the view like this

   $this->set('customers', $customers);

In the view i use this array in a form so that the user can select one

   echo $this->Form->input('customer_id', array('options' => array($customers)));

The data which is displayed in the select form is this 'Customer1', 'Customer2', 'Customer3'

Eveyting works ok so far.

Now, after the user has submited the data, i want to do some further logic in the controller. I want to take the data which the user has selected and save it in a second table. So i do this:

   $this->Invoice->set('secondtabel', $this->request->data['Invoice']['customer_id']);

The data is being saved in the second table, but the problem is saves the value '1', '2', '3' not the name of the customer. How can i save the name of the customer and not the identifier number from the array.

Please bear with me, i am new to cakephp and to php in general.

Was it helpful?

Solution

I assume this is actually a problem with your HTML, your select box likely looks something like this:

<select name="customer_id">
    <option value="1">Customer1</option>
    <option value="2">Customer2</option>
    <option value="3">Customer3</option>
</select>

This is why your values are 1, 2 or 3 and not Customer1 etc, because $this->request->data['Invoice']['customer_id'] is equal to 1, 2 or 3 etc.

My suggestion would be to fix this at the root of the problem, and I think that by only passing the values into your select box, you should get HTML like this:

<option>Customer1</option>

... which will mean that $this->request->data['Invoice']['customer_id'] will equal Customer1 etc.

So, try this: (array_values will return an array only containing the values, essentially stripping the keys)

$this->set('customers', array_values($customers));

This should fix your problem. However, as far as structured data goes, the way you are currently doing it (storing 1, 2 or 3 etc) is actually the correct way to do this. This way, you simply join the customers table when you are retrieving this data, and you can grab the name that way... Something like this:

$invoices = $this->Invoice->find('all', array(
    'conditions' => array(
        // your custom find conditions
    ),
    'joins' => array(
        array(
            'table' => 'customers',
            'alias' => 'Customer',
            'type' => 'LEFT',
            'conditions' => array('Customer.id = Invoice.customer_id')
        )
    ),
    'fields' => array(
        'Invoice.*', // retrieve regular invoice data
        'Customer.name' // retrieve the joined customer name too
    )
));

This way you still store a customer ID as an integer, and simply look up the name with SQL when you go to retrieve that data.

A possible reason why you might want to do it simply by storing the customer's name as text is that you want to store the customers name as it appears at the time, meaning if it changes in the future, previous invoices with that name attached won't change with it because that name is stored in text rather than a numeric reference to another table containing a name that has changed.

Hope this helps.

Docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top