-1

So, my problem is when I'm trying to update schema with command "dotrine:schema:update --force", entity are create but without any relation.

I have two entity "advert" and "category" and a relation ManyToMany on advert entity.

This is entyty advert :

<?php

namespace testBundle\Entity;

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

/**
 * Advert
 *
 * @ORM\Table(name="advert")
 * @ORM\Entity(repositoryClass="testBundle\Repository\AdvertRepository")
 */
class Advert
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
    * @var string
     *
     * @ORM\Column(name="categories", type="string", length=255)
     * /**

 * @ORM\ManyToMany(targetEntity="OC\PlatformBundle\Entity\Category", cascade={"persist"})

 */
    private $categories;

    public function __construct()

    {

        $this->date       = new \Datetime();

        $this->categories = new ArrayCollection();


    }


    // Notez le singulier, on ajoute une seule catégorie à la fois

    public function addCategory(Category $category)

    {

    // Ici, on utilise l'ArrayCollection vraiment comme un tableau

        $this->categories[] = $category;

    }


    public function removeCategory(Category $category)

    {

        // Ici on utilise une méthode de l'ArrayCollection, pour supprimer la catégorie en argument

        $this->categories->removeElement($category);

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

    /**
     * Set categories
     *
     * @param string $categories
     *
     * @return Advert
     */
    public function setCategories($categories)
    {
        $this->categories = $categories;

        return $this;
    }

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

My entity category :

<?php

namespace testBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="testBundle\Repository\CategoryRepository")
 */
class Category
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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


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

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

    return $this;
}

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

and this is the result of command doctrine:schema:update --force --dump-sql :

CREATE TABLE advert (id INT AUTO_INCREMENT NOT NULL, categories VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

Like you can see, no relation and no table advert_category

I found some topic about this but no solution working for me.

I hope someone can help me

Thank you !

c.DAUD
  • 1
  • 1

2 Answers2

1

Relation declared wrong, i think it should be something like this :

/**
 * @ORM\ManyToMany(targetEntity="testBundle\Entity\Category")
 * @ORM\JoinTable(name="advert_category")
 */
private $categories;
tchartron
  • 669
  • 10
  • 18
  • Yes excuse me I forget to change this , this code is in the tutorial on Openclassroom , I have an other projets with the same problem and I copy this code for chek if my code is wrong or it"s a bug. So I modify but no change , I have two table , Advert and Categorie , but not a table advert_categorie – c.DAUD Nov 05 '16 at 11:39
  • You have to remove the annotation @ORM\Column(name="categories", type="string", length=255) in your advert entity, when you add a relation with the code i gave you doctrine handle everything you don't have to create a column named "categories" by yourself, only put the relation declaration i gave you on the categories attribute – tchartron Nov 05 '16 at 11:47
0

So problem fixed, I have to delete this :

 *
 * @ORM\Column(name="categories", type="string", length=255)
/**

and it's work , thank you @thomas !

c.DAUD
  • 1
  • 1