Search code examples
formssymfony1many-to-manypropel

Unable to execute insert statement while saving in symfony (propel) form with map list


In doSave() function in form I'm trying to save article category (which it is a node in a tree), and then assign to this category articles (many to many relationship). I'm getting an error while in $this->saveWaArticleNewsCategoryMapList($con); line. (Project is builded on symfony 1.4.16). There is no error when I assign article to an existing category.

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

Error:

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

Solution

  • Updating ArticleCategoryId in object solved the problem:

    $node->save($con);
    $this->getObject()->setArticleCategoryId($node->getArticleCategoryId());
    ...