0

在表单中的 doSave() 函数中,我试图保存文章类别(它是树中的一个节点),然后分配给这个类别文章(多对多关系)。我在$this->saveWaArticleNewsCategoryMapList($con);排队时遇到错误。(项目基于 symfony 1.4.16)。将文章分配给现有类别时没有错误。

public function doSave($con = null) {

$scope = $this->getValue('tree_scope');
$toEdit = $this->getObject();
if ($toEdit->isNew()) {
    $node = new ArticleCategory();
    $node->setBrandId($this->getValue('brand_id'));
    $node->setName($this->getValue('name'));
    $node->setTreeScope($scope);

    $root = ArticleCategoryQuery::create()->findRoot($scope);
    if ($root == null) {
        $root = new ArticleCategory();
        $root->setName('Root');
        $root->setTreeScopeIdValue($scope);
        $root->makeRoot();
        $root->save($con);

        $node->insertAsLastChildOf($root);
        $node->save($con);
        return;
    }
    $parent = ArticleCategoryQuery::create()->findPk($this->getValue('parent_id'));
    $node->insertAsLastChildOf($parent);
} else {
    $node = ArticleCategoryQuery::create()->findOneByArticleCategoryId($toEdit->getArticleCategoryId());
    $node->setBrandId($this->getValue('brand_id'));
    $node->setName($this->getValue('name'));
    if ($toEdit->getParent()->getArticleCategoryId() != $this->getValue('parent_id')) {
        $parent = ArticleCategoryQuery::create()->findPk($this->getValue('parent_id'));
        $node->moveToLastChildOf($parent);
    }
}
$node->save($con);
$this->saveArticleNewsCategoryMapList($con); <--Error
$this->saveArticleHelpCategoryMapList($con);
}

错误:

Unable to execute INSERT statement [INSERT INTO `article_news_category_map` (`ARTICLE_ID`) VALUES (:p0)] [wrapped: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`projectname`.`article_news_category_map`, CONSTRAINT `article_news_category_map_FK_2` FOREIGN KEY (`category_id`) REFERENCES `article_category` (`article_category_id`) ON DELETE CASCADE)]
4

1 回答 1

0

更新ArticleCategoryId对象解决了这个问题:

$node->save($con);
$this->getObject()->setArticleCategoryId($node->getArticleCategoryId());
...
于 2012-06-11T10:51:56.793 回答