簡體   English   中英

如何在 CakePHP 中將 staff_name 列保存為其他列?

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

我正在制作分類帳應用程序來殺死文書工作。

但制作簡單的 CRUD 時,出現了問題。

其他列都可以,但一列無法保存。

你會看看這個嗎?

我只是在生成烘焙命令時添加、編輯、刪除和查看 ctp 文件。

而 Controller 也是通過 bake 命令制作的。

我只是將 POST 方法更改為 GET 方法。

試過了

  • 重新制作表格列,確保我使用正確的名稱。
  • 確保我使用正確的蛋糕語法。
  • 在實體中添加可訪問
    protected $_accessible = [
        '*' => true
    ];

添加.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>

我想制作 select 表格來限制類別。

分類帳控制器.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'));
    }

D B

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)

所有代碼都在這里

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

表名弄亂了。
我很困惑,因為由於蛋糕 php 條件而無法使用列staff_id ,所以我制作了新的列worker並替換了它,但它很復雜。 當我將代碼與staff_id集成時效果很好

我也對<th> and <td>感到困惑

                <th scope="col"><?= $this->Paginator->
                    sort('id') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('customer_name') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('customer_tel1') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('customer_tel2') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('staff_name') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('work_category') ?></th>
                <th scope="col"><?= $this->Paginator->
                     sort('created') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('reserved') ?></th>
                <th scope="col"><?= $this->Paginator->
                    sort('modified') ?></th>
                <th scope="col" class="actions">
                    <?= __('Actions') ?></th>

            <?php foreach ($ledgers as $ledger): ?>
            <tr>
                <td><?= $this->Number->format($ledger->id) ?></td>
                <td><?= h($ledger->customer_name) ?></td>
                <td><?= h($ledger->customer_tel1) ?></td>
                <td><?= h($ledger->customer_tel2) ?></td>
                <td><?= h($ledger->staff_name) ?></td>
                <td><?= h($ledger->work_category) ?></td>
                <td><?= h($ledger->created) ?></td>
                <td><?= h($ledger->reserved) ?></td>
                <td><?= h($ledger->modified) ?></td>

試試這個 select 選項可能會有所幫助。

添加.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() ?>

這種風格也可以幫助您輕松編輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM