0

我有三个表,ArticlesCommentsTags

Tags属于ArticlesComments

$this->Articles->patchEntity($entity, $this->request->getData(), [
    'associated' => ['Comments.Tags']
]);

出现以下错误:

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

Please try correcting the issue for the following table aliases:

    CommentsArticles

但是如果我只保存'associated' => ['Comments']它可以保存ArticleComments连接表关联,只是不保存任何Tags.

Articles表有这些关联:

$this->hasMany('Tags', [
  'foreignKey' => 'article_id'
]);
$this->belongsToMany('Comments', [
  'foreignKey' => 'article_id',
  'targetForeignKey' => 'comment_id',
  'joinTable' => 'comments_articles'
]);

Comments表有这些关联:

$this->hasMany('Tags', [
  'foreignKey' => 'comment_id'
]);
$this->belongsToMany('Articles', [
  'foreignKey' => 'comment_id',
  'targetForeignKey' => 'article_id',
  'joinTable' => 'comments_articles'
]);

Tagstable 有这些关联:

$this->belongsTo('Comments', [
  'foreignKey' => 'comment_id',
  'joinType' => 'INNER'
]);
$this->belongsTo('Articles', [
  'foreignKey' => 'article_id',
  'joinType' => 'INNER'
]);

这是修补后的实体看起来像这样。

object(App\Model\Entity\Article) {

    'title' => 'example article name',
    'users' => [
        '_ids' => []
    ],
    'comments' => [
        (int) 0 => object(App\Model\Entity\Comment) {
            'id' => (int) 1,
            'content' => 'this is a comment',
            'tags' => [
                (int) 0 => object(App\Model\Entity\Tag) {

                    'name' => 'example tag name',
                    '[new]' => true,
                    '[accessible]' => [
                        'comment_id' => true,
                        'article_id' => true,
                        'comment' => true,
                        'article' => true
                    ],
                    '[dirty]' => [
                        'name' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[hasErrors]' => false,
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Tags'

                }
            ],
            '[new]' => false,
            '[accessible]' => [
                'content' => true,
                'tags' => true,
                'articles' => true
            ],
            '[dirty]' => [
                'tags' => true
            ],
            '[original]' => [
                'tags' => [
                    (int) 0 => [
                        'name' => '0'
                    ]
                ]
            ],
            '[virtual]' => [],
            '[hasErrors]' => false,
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Comments'

        }
    ],
    '[new]' => true,
    '[accessible]' => [
        'title' => true,
        'tags' => true,
        'comments' => true,
        'users' => true
    ],
    '[dirty]' => [
        'title' => true,
        'users' => true,
        'comments' => true
    ],
    '[original]' => [
        'users' => []
    ],
    '[virtual]' => [],
    '[hasErrors]' => false,
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Articles'

}
4

1 回答 1

1

CaekPHP 不支持这一点,它只能在一个方向上填充直接关联的外键/。例如,您可以:

  • 预填充外键字段(当然只有在文章和/或评论已经存在时才有效)

  • 使用文章和评论记录的主键手动分别保存标签

  • 创建关联类,在保存文章时将文章主键传递给选项,并article_id在保存标签时使用它来填充字段

  • 挂钩到表级别的保存过程以传递文章主键并用它填充标签

这是后一种解决方案的一个快速而肮脏的示例,它还应该让您了解它如何在关联级别上工作:

ArticlesTable

public function beforeSave(
    \Cake\Event\Event $event,
    \Cake\Datasource\EntityInterface $entity,
    \ArrayObject $options
) {
    if (isset($options['Articles.id'])) {
        unset($options['Articles.id']);
    }
}

protected function _onSaveSuccess($entity, $options)
{
    if ($options['_primary']) {
        $options['Articles.id'] = $entity->get('id');
    }

    return parent::_onSaveSuccess($entity, $options);
}

TagsTable

public function beforeSave(
    \Cake\Event\Event $event,
    \Cake\Datasource\EntityInterface $entity,
    \ArrayObject $options
) {
    if (!$options['_primary'] &&
        isset($options['Articles.id'])
    ) {
        $entity->set('article_id', $options['Articles.id']);
    }
}
于 2019-04-26T11:28:04.400 回答