Question

I am new in Symfony2 (worked with Symfony 1 for several years) and I am trying to insert some records in a Entity with a relationship to another Entity, here are them:

<?php

namespace Jjj\SomeBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;

/**
* Jjj\SomeBundle\Entity
*
* @ORM\Table(name="content")
* @ORM\Entity(repositoryClass="Jjj\SomeBundle\Entity\ContentRepository")
*/
class Content implements Translatable
{
    /**
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="contents")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category_id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="title", type="string", length=32, nullable=false)
     */
    protected $title;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="summary", type="text", nullable=true)
     */
    protected $summary;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="fulltext", type="text", nullable=true)
     */
    protected $fulltext;

    /**
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     */
    protected $created_at;

    /**
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    protected $updated_at;

    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=128, unique=true)
     */
    private $slug;

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

    /**
     * Set category_id
     *
     * @param integer $categoryId
     * @return Content
     */
    public function setCategoryId($categoryId)
    {
        $this->category_id = $categoryId;

        return $this;
    }

    /**
     * Get category_id
     *
     * @return integer 
     */
    public function getCategoryId()
    {
        return $this->category_id;
    }

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

        return $this;
    }

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

    /**
     * Set slug
     *
     * @param string $slug
     * @return Content
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * Set summary
     *
     * @param string $summary
     * @return Content
     */
    public function setSummary($summary)
    {
        $this->summary = $summary;

        return $this;
    }

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

    /**
     * Set fulltext
     *
     * @param string $fulltext
     * @return Content
     */
    public function setFulltext($fulltext)
    {
        $this->fulltext = $fulltext;

        return $this;
    }

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

    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return Content
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param \DateTime $updatedAt
     * @return Content
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;

        return $this;
    }

    /**
     * Get updated_at
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }
}

This is the Category Entity:

<?php

namespace Jjj\SomeBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
* Jjj\SomeBundle\Entity
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="Jjj\SomeBundle\Entity\CategoryRepository")
*/
class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=32, nullable=false)
     */
    protected $title;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255, nullable=false)
     */
    protected $description;

    /**
     * @var string
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=false)
     */
    protected $created_at;

    /**
     * @var string
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=false)
     */
    protected $updated_at;

    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=128, unique=true)
     */
    private $slug;

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

    public function __construct()
    {
        $this->contents = new ArrayCollection();
    }

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

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

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     * @return Category
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return Category
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param \DateTime $updatedAt
     * @return Category
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;

        return $this;
    }

    /**
     * Get updated_at
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Category
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * Add contents
     *
     * @param \Jaguar\AloBundle\Entity\Content $contents
     * @return Category
     */
    public function addContent(\Jaguar\AloBundle\Entity\Content $contents)
    {
        $this->contents[] = $contents;

        return $this;
    }

    /**
     * Remove contents
     *
     * @param \Jjj\SomeBundle\Entity\Content $contents
     */
    public function removeContent(\Jjj\SomeBundle\Entity\Content $contents)
    {
        $this->contents->removeElement($contents);
    }

    /**
     * Get contents
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getContents()
    {
        return $this->contents;
    }
}

In the controller that is running, I wrote this:

$content = new Content();
$content->setTitle('Content Example');
$content->setSummary('Content Example');
$content->setFulltext('My first content...');
$content->setCategoryId(2);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($content);
$em->flush();

The error says:

ContextErrorException: Warning: spl_object_hash() expects parameter 1 to be object, integer given in D:\xampp\htdocs\projects\alopatria\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1367

I was googling for a while but no luck found.

Any help, please?

Thanks!

Was it helpful?

Solution

You’re doing the mapping in Content.php in the wrong way.

If you’re setting a ManyToOne relation with the Category Entity, you should have a $category and not a $category_id attribute. You should deal with objects, and not integers.

Therefore, your Content Entity should look like this:

<?php

// Jjj\SomeBundle\Entity\Content.php

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="contents")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

/**
 * Set category
 *
 * @param \Jjj\SomeBundle\Entity\Category $category
 * @return Content
 */
public function setCategory($category)
{
    $this->category = $category;

    return $this;
}

/**
 * Get category
 *
 * @return category
 */
public function getCategory()
{
    return $this->category;
}

And your controller like this:

<?php

$category = new Category();
//… $category->setTitle() and so on…

$content = new Content();
$content->setTitle('Content Example');
$content->setSummary('Content Example');
$content->setFulltext('My first content...');

$content->setCategory($category);

$em = $this->getDoctrine()->getEntityManager();
$em->persist($category);
$em->persist($content);
$em->flush();

You will then be able to access the id of a Category Entity (already fetched from database) with:

$categoryId = $category->getId();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top