PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] How can I save staff_name column as others in CakePHP?

 January 11, 2022     cakephp, database, mysql, php     No comments   

Issue

I am making ledger app to kill the paper work.

but making simple CRUD, there was a problem.

Other columns are OK, but one column cannot be saved.

Would you take a look at this?

I am just making add, edit, delete, and view ctp files as bake command generated .

And the Controller also made by bake command.

I just changed POST method to GET method.

tried

  • re-making table columns, make sure I am using correct name.
  • make sure I am using correct cake grammar.
  • add accessible in Entity
    protected $_accessible = [
        '*' => true
    ];

add.ctp

<div class="ledgers form large-9 medium-8 columns content">
    <?= $this->Form->create($ledger) ?>
    <fieldset>
        <legend><?= __('Add Ledger') ?></legend>
        <?php
            $men = [
                'Fukuda','Hayasi','Seki','Kubo'
            ];
            $work_category = [
                'preview','build','repair','etc'
            ];

            echo $this->Form->control('customer_name');
            echo $this->Form->control('customer_adress');
            echo $this->Form->control('customer_tel1');
            echo $this->Form->control('customer_tel2');

            // need arr, $members = [hukuda, hayasi,ryo]
            echo $this->Form->control('staff_name', 
                ['options' => $men,
            ]);
            echo $this->Form->control('work_category',
                ['options' => $work_category,
            ]);
            echo $this->Form->control('content');
            echo $this->Form->control('reserved');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

I want to make select form to restrict categories.

LedgersController.php

    public function edit($id = null) {
        // return the GET data (in url)
        $ledger = $this->Ledgers->get($id);

        // this is POST only -------------
        if ($this->request->is(['patch', 'post', 'put'])) {

            $ledger = $this->Ledgers->patchEntity(
                $ledger, $this->request->getData());
            if ($this->Ledgers->save($ledger)) {
                $this->Flash->success(
                    __('The ledger has been saved.'));

                return $this->redirect(
                    ['action' => 'index']);
            }
            $this->Flash->error(
                __('The ledger could not be saved.'));
        }
        // POST
        $this->set(compact('ledger'));
    }

DB

MariaDB [fesa]> desc ledgers;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| customer_name   | varchar(64)  | NO   |     | NULL    |                |
| customer_adress | text         | NO   |     | NULL    |                |
| customer_tel1   | varchar(64)  | NO   |     | NULL    |                |
| customer_tel2   | varchar(64)  | NO   |     | NULL    |                |
| work_category   | varchar(255) | NO   |     | NULL    |                |
| content         | text         | NO   |     | NULL    |                |
| created         | date         | NO   |     | NULL    |                |
| reserved        | date         | NO   |     | NULL    |                |
| modified        | date         | NO   |     | NULL    |                |
| staff_name      | varchar(64)  | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

all code is here

https://github.com/kaede0902/cake3/tree/master/ledger/src


Solution

Try this select option may help.

add.ctp

<div class="ledgers form large-9 medium-8 columns content">
<?= $this->Form->create($ledger) ?>
<fieldset>
    <legend><?= __('Add Ledger') ?></legend>
    <?php

        echo $this->Form->control('customer_name');
        echo $this->Form->control('customer_adress');
        echo $this->Form->control('customer_tel1');
        echo $this->Form->control('customer_tel2');
        echo $this->Form->control('staff_name', [
            'type' => 'select',
            'placeholder' => __('staff_name'),
            'label' => __('staff_name'),
            'empty' => __('(Choose Type)'),
            'value' => $ledger->staff_name,
            'options' => [
                        ['value' => 'Fukuda', 'text' => __('Fukuda')],
                        ['value' => 'Hayasi', 'text' => __('Hayasi')],
                        ['value' => 'Seki', 'text' => __('Kubo')],
                        ['value' => 'Kubo', 'text' => __('Kubo')]
                    ]
        ]);

        echo $this->Form->control('work_category ', [
            'type' => 'select',
            'placeholder' => __('work_category '),
            'label' => __('work_category'),
            'empty' => __('(Choose Type)'),
            'value' => $ledger->work_category ,
            'options' => [
                        ['value' => 'preview', 'text' => __('preview')],
                        ['value' => 'build', 'text' => __('build')],
                        ['value' => 'repair', 'text' => __('repair')],
                        ['value' => 'etc', 'text' => __('etc')]
                    ]
        ]);
        echo $this->Form->control('content');
        echo $this->Form->control('reserved');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

this style also help you for easy editing too.



Answered By - Touch sophonara
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing