0
votes

I'm trying to save data with ManyToOne relations in a DataFixtures class. And I get this error on saving data.

Please, help me.

Sources:

Entity/DealsCategory.php:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * DealsCategory
 *
 * @ORM\Table(name="deals_category")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DealsCategoryRepository")
 */
class DealsCategory
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="alias", type="string", length=50, nullable=true)
     */
    private $alias;

    /**
     * @ORM\OneToMany(targetEntity="DealsCategoryLang", mappedBy="category")
     */
    protected $categoryLang;

    /**
     * @return mixed
     */
    public function getCategoryLang()
    {
        return $this->categoryLang;
    }

    /**
     * @param DealsCategoryLang $categoryLang
     */
    public function setCategoryLang(DealsCategoryLang $categoryLang = null)
    {
        $this->categoryLang = $categoryLang;
    }

    /**
     * @param DealsCategoryLang $categoryLang
     */
    public function setOneCategoryLang(DealsCategoryLang $categoryLang)
    {
        $this->categoryLang[] = $categoryLang;
    }

    /**
     * DealsCategory constructor.
     */
    public function __construct()
    {
        $this->categoryLang = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set alias
     *
     * @param string $alias
     *
     * @return DealsCategory
     */
    public function setAlias($alias)
    {
        $this->alias = $alias;

        return $this;
    }

    /**
     * Get alias
     *
     * @return string
     */
    public function getAlias()
    {
        return $this->alias;
    }
}

Entity/DealsCategoryLang.php:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * DealsCategoryLang
 *
 * @ORM\Table(name="deals_category_lang")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DealsCategoryLangRepository")
 */
class DealsCategoryLang
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column(name="catId", type="integer")
     */
    private $catId;

    /**
     * @var string
     *
     * @ORM\Column(name="lang", type="string", length=10)
     */
    private $lang;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=70)
     */
    private $title;

    /**
     * @ORM\ManyToOne(targetEntity="DealsCategory", inversedBy="categoryLang", cascade={"persist"})
     * @ORM\JoinColumn(name="catId", referencedColumnName="id")
     *
     */
    protected $category;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set catId
     *
     * @param integer $catId
     *
     * @return DealsCategoryLang
     */
//    public function setCatId($catId)
//    {
//        $this->catId = $catId;
//
//        return $this;
//    }

    /**
     * Get catId
     *
     * @return int
     */
    public function getCatId()
    {
        return $this->catId;
    }

    /**
     * Set lang
     *
     * @param string $lang
     *
     * @return DealsCategoryLang
     */
    public function setLang($lang)
    {
        $this->lang = $lang;

        return $this;
    }

    /**
     * Get lang
     *
     * @return string
     */
    public function getLang()
    {
        return $this->lang;
    }

    /**
     * Set title
     *
     * @param string $title
     *
     * @return DealsCategoryLang
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }
}

DataFixtures/ORM/LoadCategories.php

<?php
namespace AppBundle\DataFixtures\ORM;


use AppBundle\Entity\DealsCategory;
use AppBundle\Entity\DealsCategoryLang;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadCategories implements FixtureInterface, ContainerAwareInterface
{

    private $container;
    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager)
    {
        $category = new DealsCategory();
        $categoryLang = new DealsCategoryLang();
        $categoryLang->setLang('en');
        $categoryLang->setTitle('Food and Drinks');
        $category->setOneCategoryLang($categoryLang);

        $manager->persist($category);
        $manager->persist($categoryLang);
        $manager->flush();
    }

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
}

I've tried it in different ways, but it's still not working. Tell me please, what i am doing wrong?

UPD: my errors are:

[Doctrine\DBAL\Exception\NotNullConstraintViolationException]
An exception occurred while executing 'INSERT INTO deals_category_lang (catId, lang, title) VALUES (?, ?, ?)' with params [null, "en", "Food and Drinks"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'catId' cannot be null

[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'catId' cannot be null

PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'catId' cannot be null

I am running the fixtures through the command line:

php bin/console doctrine:fixtures:load --append 
1
Are you getting any errors? In what way does it not work?Phil
The error you getting is self explanatory, your catid is nullMasivuye Cokile
Updated info about the errors, I got.montie

1 Answers

0
votes

Ok, I resolved it... The problem was in my Fixture class:

Instead of

$category->setOneCategoryLang($categoryLang);

I should put the

$categoryLang->setCategory($category);

,respecting the hierarchy of my entities' relations :(