在编辑或创建模型时设置从另一个 table 加入 table 的默认值

Setting a default value on join table from another table when model is edited or created

希望有人能给我指出正确的方向。

我有一个包含(除其他外)推荐和评估的应用程序。它们通过连接 table 加入,其中包含我想更新但正在努力弄清楚如何更新的额外字段。

正如您在上面看到的,当我创建推荐时,我设置了以下字段:

现在,当我创建一个新的评估或编辑一个没有任何建议的评估时 linked 评估可以很好地保存,因为不需要将任何内容写入连接 table。

当我尝试编辑评估以包含一个或多个建议时,应用程序尝试将 link 写入联接 table 但失败了,因为 user_impact、business_impact、deployment_complexity 和关键性字段未指定 - 完全正常,因为我已将字段设置为 MySQL 中的必需字段,对吧?我在 CakePHP 中得到的错误是

SQLSTATE[HY000]: General error: 1364 Field 'user_impact' doesn't have a default value

我想要做的是在编辑或创建评估时使用建议 table 中的值来填充相应的加入 table 条目。有什么想法吗?

举个例子:

我想这样做的原因是我可以为这些字段设置建议值,然后如果用户想要 运行 评估并且他们只想调整值他们自己的评估然后它不会影响其他人等等

这是我的 AssessmentsTable 关联。

    $this->belongsToMany('Recommendations', [
        'foreignKey' => 'assessment_id',
        'targetForeignKey' => 'recommendation_id',
        'joinTable' => 'assessments_recommendations',
        'through' => 'assessments_recommendations',
    ]);

这是我的 RecommendationsTable 关联。

    $this->belongsToMany('Assessments', [
        'foreignKey' => 'recommendation_id',
        'targetForeignKey' => 'assessment_id',
        'joinTable' => 'assessments_recommendations',
        'through' => 'assessments_recommendations',
    ]);

这是我的 AssessmentsRecommendations 协会:

    $this->belongsTo('Assessments', [
        'foreignKey' => 'assessment_id',
        'joinType' => 'INNER',
    ]);
    $this->belongsTo('Recommendations', [
        'foreignKey' => 'recommendation_id',
        'joinType' => 'INNER',
    ]);

这是我的 AssessmentsController 编辑功能的样子:

public function edit($id = null)
{
    $assessment = $this->Assessments->get($id, [
        'contain' => ['Recommendations'],
    ]);

    if ($this->request->is(['patch', 'post', 'put'])) {
        $assessment = $this->Assessments->patchEntity($assessment, $this->request->getData(), ['associated'=>['Recommendations._joinData']]);            
        if ($this->Assessments->save($assessment, ['associated' => ['Recommendations._joinData']])) {
            $this->Flash->success(__('The assessment has been saved.'));       

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The assessment could not be saved. Please, try again.'));
    }
    $clients = $this->Assessments->Clients->find('list', ['limit' => 200]);
    $recommendations = $this->Assessments->Recommendations->find('list', ['limit' => 200]);
    $this->set(compact('assessment', 'clients', 'recommendations'));
}

现在,当我将 beforeSave 函数添加到 AssessmentsRecommendationsTable 时,我看到以下错误:

Argument 2 passed to App\Model\Table\AssessmentsRecommendationsTable::beforeSave() must be an instance of App\Model\Table\EntityInterface, instance of Cake\ORM\Entity given, called in /var/www/html/csa-portal/vendor/cakephp/cakephp/src/Event/EventManager.php on line 310

如有任何帮助,我们将不胜感激。

您是否考虑过编写一个规则来处理这个问题?

https://book.cakephp.org/3/en/orm/validation.html#applying-application-rules

首先,你使用的联想是错误的。应该是这样的

For AssessmentsTable

   $this->hasMany('AssessmentsRecommendations', [
        'foreignKey' => 'assessment_id'
   ]);

For RecommendationsTable

   $this->hasMany('AssessmentsRecommendations', [
        'foreignKey' => 'recommendation_id'
   ]);

For AssessmentsRecommendationsTable

    $this->belongsTo('Assessments', [
        'foreignKey' => 'assessment_id',
        'joinType' => 'INNER',
    ]);

    $this->belongsTo('Recommendations', [
        'foreignKey' => 'recommendation_id',
        'joinType' => 'INNER',
    ]);

现在对于默认值,您必须在 AssessmentsRecommendationsTable.php 中使用 beforeSave file.You 可以根据需要修改您的数据保存前这里。

public function beforeSave(Event $event, EntityInterface $entity, \ArrayObject $options)
    {
        if ($entity->isNew()) { // Returns true when you add new record
            $recommendation = TableRegistry::getTableLocator()->get('Recommendations')->get($entity->recommendation_id);
            $entity->user_impact = $recommendation->default_user_impact;
            $entity->business_impact = $recommendation->default_business_impact;
            $entity->deployment_complexity = $recommendation->default_deployment_complexity;
            $entity->criticality = $recommendation->default_criticality;
        }
    }
  • 我从来没有使用过 belongsToMany,如果关联对你有效,那么请忽略关联部分。