Cakephp 3: Getting array in form control in view.ctp

My form shows the associated table array. I need the form to just show the title of the item and to input the id number in the foreign key column.

The form currently shows the array of the associated table but I can not successfully save anything. This does not require me to save anything in the associated tables.

I have an add.ctp for a BudgetExpenses Table. It has two associations. An ExpensesTitleTable and an AnnualOperatingBudgetTable. The Budget Expenses table has a annual_operating_budgets_id column and a expense_titles_id.

here is the add function in the BudgetExpensesController

public function add()
    {
        $budgetExpense = $this->BudgetExpenses->newEntity();
        if ($this->request->is('post')) {
            $budgetExpense = $this->BudgetExpenses->patchEntity($budgetExpense, $this->request->getData(), ['associated' => [ 'ExpenseTitles',  'AnnualOperatingBudgets']]);
            if ($this->BudgetExpenses->save($budgetExpense)) {
                $this->Flash->success(__('The budget expense has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The budget expense could not be saved. Please, try again.'));
        }
        $annualOperatingBudgets = $this->BudgetExpenses->AnnualOperatingBudgets->find('all');
        $expenseTitles = $this->BudgetExpenses->ExpenseTitles->find('all');
        $this->set(compact('budgetExpence', 'expenseTitles', 'annualOperatingBudgets'));
    }

The add.ctp looks like this:

<div class="budgetExpenses form large-9 medium-8 columns content">
             <?= $this->Form->create($budgetExpense) ?>
            <fieldset>
                <legend><?= __('Add Budget Expense') ?></legend>
                <?php
                    echo $this->Form->control('annual_operating_budgets_id', ['label' => 'Budget Year & Institution'], ['options' => $annualOperatingBudgets ]);
                    echo $this->Form->control('expense');
                    echo $this->Form->control('expense_title_id');
                ?>
            </fieldset>
            <?= $this->Form->button(__('Submit')) ?>
            <?= $this->Form->end() ?>
        </div>

In the browser I have a drop down picker and looks like this:
addbudgetexpenses-view

The error is from the:

<?= $this->Form->create($budgetExpense) ?>

you have typo
budgetExpence vs $budgetExpense

for the question about id, name pair:
https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

1 Like

yes fixed typo :blush: and the error went away, thanks for that.

Key pairs do get the value and the label correct.
so the keyValue= id and the valueField= expense_title.
but when I hit submit I get:
The budget expense could not be saved. Please, try again.

check your debug($budgetExpense) after patchEntity if there are any validation errors

I’m getting a message SQLSTATE[HY000]: General error: 1364 Field ‘annual_operating_budgets_id’ doesn’t have a default value

it looks like the SQL is only trying to insert the expense but not the annual_operating_budget_id or the expense_title_id.
SQL Log shows:
BEGIN
0 0
SELECT
1 AS existing
FROM
expense_titles ExpenseTitles
WHERE
ExpenseTitles.id = 2
LIMIT
1
1 0
INSERT INTO budget_expenses (expense)
VALUES
(‘290.100000’)

ExpenseTitles.id is correct the value is 2 and for annual_operating_budgets_id should be 1 in this case.

check for https://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment maybe you have it blocked for patchEntity

EDIT:
also you could set it via ->Form->control('annual_operating_budget.id') so not directly but by associated model https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongsto-associations

I found the mistake. annual_operating_budgets_id is the name in the mysql. once I added the s after budgets it works. Thanks so much for your time. You were a BIG help.