8719 sujets

Développement web côté serveur, CMS

Bonjour à tous,

J'ai un souci de récupération de mes données. En effet, j'ai 2 entités : Theme et Category qui sont liées par une relation ManyToOne bidirectionnelle. Ça devrait être une relation ManyToMany mais la base de données a été conçue comme ça et je ne peut pas revenir là-dessus. Mon problème quand j'essaye de récupérer mes infos du côté inverse de la relation je n'obtiens rien. Mes requêtes me renvoient un array vide et je ne sais pas comment contourner ce problème. J'avoue c'est un problème particulier car dans mon entité propriétaire Category qui contient le champ (qui est supposé être la clé étrangère) qui est un type varchar(100) et qui contient les identiants des Thèmes (Entité Theme). Voici un exemple du contenu de ce champ :

theme_id
29,34
29,31
29
21
29,34
25,33,34

Mon Controller est le suivant :

class PostController extends Controller
{
    public function viewAction(Request $request)
    {
        $id = $request->query->get('id');
        $em = $this->getDoctrine()->getManager();
        $tab = [];
        $themes = $em->getRepository(Theme::class)->findBy(array('id' => $id));
        foreach ($themes as $theme) {
            $theme = $em->getRepository(Theme::class)->find($theme->getId());
            foreach ($theme-> getCategories() as $cats) {
                if ($cats != null) {
                    array_push($tab, $cats);
                }
            }
        }
 
        return $this->render('content.html.twig', array(
            'themes' => $themes,
            'cats' => $tab
        ));
 
    }
}


Ma vue

{% for theme in themes %}
    <h2>{{ theme.name }}</h2>
 
    {% for cat in theme.cats %}
        <div>
            <p>{{ cat.title }}</p>
            <p>{{ cat.content }}</p>
        </div>
    {% endfor %}
{% endfor %}


Mes entités:

// Entité Theme:
namespace AppBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * Theme
 *
 * @ORM\Table(name="theme")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ThemeRepository")
 */
class Theme
{
    /**
     * @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;
 
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Category", mappedBy="theme")
     */
    private $categories;
 
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Set name
     *
     * @param string $name
     *
     * @return Theme
     */
    public function setName($name)
    {
        $this->name = $name;
 
        return $this;
    }
 
    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    }
 
    /**
     * Add category
     *
     * @param \AppBundle\Entity\Category $category
     *
     * @return Theme
     */
    public function addCategory(\AppBundle\Entity\Category $category)
    {
        $this->categories[] = $category;
        $this->categories->add($category);
        $category->setTheme($this);
 
        return $this;
    }
 
    /**
     * Remove category
     *
     * @param \AppBundle\Entity\Category $category
     */
    public function removeCategory(\AppBundle\Entity\Category $category)
    {
        $this->categories->removeElement($category);
    }
 
    /**
     * Get categories
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCategories()
    {
        return $this->categories;
    }
}


// Entité Category : 
namespace AppBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="AppBundle\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="title", type="string", length=255)
     */
    private $title;
 
    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     */
    private $content;
 
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Theme", inversedBy="categories")
     * @ORM\JoinColumn(name="id", referencedColumnName="id")
     */
    private $theme;
 
    /**
     * Get id
     *
     * @return int
     */
    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 content
     *
     * @param string $content
     *
     * @return Category
     */
    public function setContent($content)
    {
        $this->content = $content;
 
        return $this;
    }
 
    /**
     * Get content
     *
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }
 
    /**
     * Set theme
     *
     * @param \AppBundle\Entity\Theme $theme
     *
     * @return Category
     */
    public function setTheme(\AppBundle\Entity\Theme $theme = null)
    {
        $this->theme = $theme;
 
        return $this;
    }
 
    /**
     * Get theme
     *
     * @return \AppBundle\Entity\Theme
     */
    public function getTheme()
    {
        return $this->theme;
    }
}


Merci par avance !
Modifié par mecano31 (10 Jun 2018 - 20:03)