【问题标题】:How can I save staff_name column as others in CakePHP?如何在 CakePHP 中将 staff_name 列保存为其他列?
【发布时间】:2019-10-03 14:56:12
【问题描述】:

我正在制作分类帐应用程序来消除文书工作。

但是制作简单的 CRUD 时,出现了问题。

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

你会看看这个吗?

我只是在添加、编辑、删除和查看 ctp 文件 作为烘焙命令生成。

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

我刚刚将 POST 方法更改为 GET 方法。

试过

  • 重新制作表格列,确保我使用了正确的名称。
  • 确保我使用的是正确的蛋糕语法。
  • 在实体中添加可访问性
    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>

我想制作选择表单来限制类别。

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'));
    }

数据库

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 mysql database cakephp


    【解决方案1】:

    试试这个选择选项可能会有所帮助。

    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() ?>
    

    这种风格也可以帮助您轻松编辑。

    【讨论】:

    • 非常感谢!!它是如此......美丽。
    • 使用您的代码,(类型、占位符、标签和空)将包含在选择选项中,所以我只使用了空和选项。
    【解决方案2】:

    表名弄乱了。
    我很困惑,因为 staff_id 列由于蛋糕 php 条件而无法使用, 所以我创建了新专栏worker 并替换了它,但这很复杂。 当我将代码与staff_id 集成时效果很好

    我也被&lt;th&gt; and &lt;td&gt;搞糊涂了

                    <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>
    

    【讨论】:

    • 您对&lt;th&gt;&lt;td&gt; 有什么困惑?
    • 我以为是数据,td 是标题。
    • 它们的命名不像其他一些表格标签那样直观,例如&lt;tbody&gt;&lt;tfoot&gt;,因此它们很容易混淆。 &lt;th&gt; 实际上代表“表头”,&lt;td&gt; 代表“表数据”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    相关资源
    最近更新 更多