Question

I have a model called Category which hasMany Expense. I am trying to generate an accordion type interface where a user can expand a category and edit the expenses. I can generate the input fields but they don't seem to be in "edit" mode because they are not pre-populated.

I've searched online and found a few related articles such as this one CakePHP: How to update multiple records at the same time with the Form helper and also this one CakePHP: Form helper with saveMany() to edit multiple rows at once. I have tried to emulate their code but with no success.

My CategoriesController Index function looks like this ...

public function index($project_id) {

    $data = $this->Category->find('all', array(
        'conditions'=>array('Category.project_id' => $project_id),
        'order'=>array('Category.category_name')
    ));
    $this->set('categories', $data);
    $this->request->data = $data;
}

I have read that cakephp2 requires $this->request->data to be set for FormHelper to work. But in the examples I have found online everyone seems to use the plural of the model name so I tried that as well.

My Categories\index.ctp looks like this. I'm not at the "accordion" stage yet. I'm just trying to get input boxes on the screen that are pre-populated.

$i=0;
foreach ($categories as $category)
{
    echo $this->Form->input("Category.$i.category_id");
    echo $this->Form->input("Category.$i.category_name");
    $j=0;
    foreach($category['Expense'] as $expense)
    {
        echo $this->Form->input('Expense.' . $j . '.expense_id');
        echo $this->Form->input('Expense.' . $j . '.expense_name');
        echo $this->Form->input('Expense.' . $j . '.dollar_amount');
        echo $this->Form->input('Expense.' . $j . '.sqft_amount');
        $j++;
    }
    $i++;
}

This code seems to be iterating properly because it spits out the correct input fields. The big problem right now is just getting the fields to pre-populate. It doesn't seem to be in "edit" mode and I'm worried that that will be a problem down the road when I try to save the data.

Also, I have tried it with and without $this->form->create('Category') at the top. It doesn't seem to make a difference.

The $categories array looks like this ...

array(
    (int) 0 => array(
        'Category' => array(
            'category_id' => '1',
            'category_name' => 'Category 1',
            'category_index' => '1',
            'project_id' => '1'
        ),
        'Project' => array(
            'project_id' => '1',
            'project_name' => '131 Anndale Dr',
            'project_sqft' => '1700',
            'project_cost' => '318',
            'project_cost_per_sqft' => '0'
        ),
        'Expense' => array(
            (int) 0 => array(
                'expense_id' => '2',
                'expense_name' => 'Nails',
                'category_id' => '1',
                'dollar_amount' => '50',
                'sqft_amount' => '1',
                'expense_index' => '2'
            ),
            (int) 1 => array(
                'expense_id' => '1',
                'expense_name' => 'Wood',
                'category_id' => '1',
                'dollar_amount' => '99',
                'sqft_amount' => '1',
                'expense_index' => '1'
            )
        )
    ),
    (int) 1 => array(
        'Category' => array(
            'category_id' => '3',
            'category_name' => 'Category 2',
            'category_index' => '2',
            'project_id' => '1'
        ),
        'Project' => array(
            'project_id' => '1',
            'project_name' => '131 Anndale Dr',
            'project_sqft' => '1700',
            'project_cost' => '318',
            'project_cost_per_sqft' => '0'
        ),
        'Expense' => array(
            (int) 0 => array(
                'expense_id' => '3',
                'expense_name' => 'Bed',
                'category_id' => '3',
                'dollar_amount' => '99',
                'sqft_amount' => '2',
                'expense_index' => '1'
            ),
            (int) 1 => array(
                'expense_id' => '4',
                'expense_name' => 'Chair',
                'category_id' => '3',
                'dollar_amount' => '70',
                'sqft_amount' => '1',
                'expense_index' => '2'
            )
        )
    )
)

Any help would be greatly appreciated. Thanks!!

Was it helpful?

Solution

Coincidentally, I've been doing almost exactly the same thing today.

In order to automatically populate the form, you need to have your data in $this->data in the view. You can do this by assigning to $this->request->data in your controller.

Then you need to have your field names reflect the structure of that array exactly. So using your example, you might have:

echo $this->Form->input("$i.Category.category_name");

for the category fields, and

echo $this->Form->input("$i.Expense.$j.expense_id");

for your expense fields.

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