【问题标题】:CakePHP: Adding default faculty/department values while adding a new UniversityCakePHP:在添加新大学时添加默认教师/部门值
【发布时间】:2017-03-29 18:49:27
【问题描述】:

使用 CakePHP 的新程序员,我有 3 个控制器:大学、学院、系。

我的桌子是: 大学 (id, name, country_id), 院系(ID,名称,大学ID), 部门(id、name、faculty_id)。

当我添加一所新大学时,我需要自动创建一个默认值 ('n/a') 作为该大学的学院,并为此创建的学院创建一个默认值 ('n/a') 作为部门。 在我们的系统中,我们需要大学至少有一名教师和一名教师。部门。因此,如果新的大学 id = 10,那么在保存后,我需要创建一个新的学院(比如说 facculties.id = 25),其中 university_i=10。和一个新部门,faculty_id = 25。

这是我的代码 UniversitiesController 添加功能

$university = $this->Universities->newEntity();

if ($this->request->is('post')) {
   $university = $this->Universities->patchEntity($university, $this->request->data);
   if ($this->Universities->save($university)) {
       $this->Flash->success(__('The university has been saved.'));
       return $this->redirect(['action' => 'index']);
   } else {
       $this->Flash->error(__('The university could not be saved. Please, try again.'));
   }
}

有什么帮助吗?

【问题讨论】:

  • 到目前为止您尝试过什么?您是否在线阅读过非常详细的 CakePHP 书籍?你有什么具体问题?您使用的是什么版本的 CakePHP? ..等等等等等等。
  • 我认为您的问题是关于设置默认值?一个真正简单的方法是在数据库级别为字段设置默认值。然后,如果您没有在$this->request->data 中设置该字段,它将保存为默认数据库值。 ALTER TABLE my_table` 更改列 my_column my_column varchar(10) NOT NULL DEFAULT 'n/a' ;`
  • 此外,如果您设置了关联,您可以执行 ->newEntity($data, ['associated' => ['Faculty', 'Department']) 或 ->patchEntity($entity, $data , ['关联' => ['教师', '部门'])
  • Derek:您的建议似乎不错,并且已经建立了关联。但是我应该在哪里将教师设置为默认值('n/a'),然后将部门设置为'n/a'?

标签: cakephp cakephp-3.0


【解决方案1】:

如果我理解正确的话,在大学或学院列表中,您希望将默认值(列表的第一个值)设为 (n/a)。如果是这样的话,我有同样的问题,答案很简单。在将大学列表作为第一个值的表中,添加 n/a 作为值,我建议保持原样,以便表上的第一个值或 SQL 结果将显示为默认值。

在您的 file.ctp 的选项列表中,您只需添加这样的值...

echo $this->Form->control('universities', [
                'options' => $universities,
                'title' => 'Select the university',
                'alt' => 'Select the university',
                'label' => 'Select the university',
                'class'=> 'classForOption',
                'empty' => false
            ]);

作为 'empty' => false 你的问题的答案。我希望!

问候

【讨论】:

  • 谢谢红。其实我的要求不一样。我有 3 个表:大学(id,name,country_id),faculties(id,name,unique_id),部门(id,name,faculty_id)。当我创建一所新大学时,我需要为其自动创建一个“n/a”学院,而对于这个学院,我需要创建一个“n/a”系,因为在我们的系统中,我们需要该大学至少拥有一个系和一个系。
  • 我明白了,在这种情况下很简单,在你的院系表和部门表中添加 n/a 并将其作为你表上的第一个元素,当涉及到创建一所新大学时使用我发布的代码,每次创建大学时都会添加一个“n/a”值。看,当您在视图中创建大学时,如果用户没有选择任何值,Form-> 控件将自动创建一个“n/a”值。此外,即使您正在验证模型,控制器也会将其传递并写入数据库。你必须确保你正在实现关系数据库
【解决方案2】:

我找到了解决方案。在 UniversitiesCotroller,

public function add()
{
    $university = $this->Universities->newEntity();
    $faculty->_joinData = $this->Universities->Faculties->newEntity();
    $faculty->_joinData->name = 'N/A';
    $university->dirty('faculties', true);


    if ($this->request->is('post')) {
        $university = $this->Universities->patchEntity($university, $this->request->data);
        if ($this->Universities->save($university, ['associated' => ['Faculties']])) {
            $this->Flash->success(__('The university has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The university could not be saved. Please, try again.'));
        }
    }
    $countries = $this->Universities->Countries->find('list');
    $this->set(compact('university', 'countries'));
    $this->set('_serialize', ['university']);
}

在大学中添加.ctp:

<?= $this->Form->create($university) ?>
    <fieldset>
        <legend><?= __('Add University') ?></legend>
        <?php
            echo $this->Form->input('name');
            echo $this->Form->input('short_name');
            echo $this->Form->input('country_id', ['options' => $countries]);
            echo $this->Form->input('town');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>

这样,当我们添加一所新大学时,它会在其院系下默认创建一个新条目('N/A')。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2016-08-03
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多