Manual ManyToMany relationship Creating middle class and ManyToOne shows Undefined index: x Error when pulling data from one class. #6462

Closed
opened 2026-01-22 15:33:38 +01:00 by admin · 1 comment
Owner

Originally created by @mmoradeveloper on GitHub (May 8, 2020).

Bug Report

PHP 7.4.5
Symfony 4.4
"symfony/orm-pack": "^1.0",
"doctrine/doctrine-bundle": "v2.0.8",
"doctrine/orm": "v2.7.2",

Summary

We generate manually a behaviour ManyToMany because we need the ability to generate more content into the middle class. So we have Product Related to Tags. and all we want is to get the list of tags from a call into ProductEntity we receive an Exception: "Notice: Undefined index: tag"

We have the following Classes:
ProductEntity

/**
 * @ORM\Table(name="product")
 * @ORM\Entity
 */
class ProductEntity
{
     
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Product\DomainModel\ProductTagRelationEntity", mappedBy="product")
     * @EXCLUDE
     */
    private $tags;

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    private $id;

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

    public function __construct()
    {
        $this->tags = new ArrayCollection();
    }
    /**
     * @return ArrayCollection
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * @return Array
     */
    public function getTagsArray()
    {
        $tags = [];
        foreach ($this->tags as $t) {
            $tags[] = [
                "id" => $t->getTag()->getId(),
                "link_id" => $t->getId(),
                "title" => $t->getTag()->getTitle(),
                "type" => $t->getTag()->getType()
            ];
        }
        return $tags;
    }
}

ProductTagRelationEntity

/**
 * @ORM\Table(name="product_tag_relation")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="App\Repository\Product\Tag\TagRepository")
 */
class ProductTagRelationEntity{

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Product\DomainModel\ProductEntity" , inversedBy="tags")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="SET NULL")
     * })
     * @EXCLUDE
     */
    private $product;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Product\DomainModel\ProductTagEntity" , inversedBy="tags")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="SET NULL")
     * })
     * @EXCLUDE
     */
    private $tag;

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    private $id;

    /**
     * Get tag
     *
     * @return ProductTagEntity $tag
     */
    public function getTag()
    {
        return $this->tag;
    }
}

And the ProductTagEntity

/**
 * App\Entity\ProductTag\ProductTag
 * @ORM\Table(name="product_tag")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="App\Repository\ProductTag\ProductTagRepository")
 */
class ProductTagEntity {

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Product\DomainModel\ProductTagRelationEntity", mappedBy="tag")
     * @EXCLUDE
     */
    private $tags;
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    private $id;

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

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

`

Current behavior

So when we get a product from a repository call;
$product = $this->productRepository->getBySlug($slug);
and when we call the function
$productEntity->getTagsArray();

the run stops in the line where we call "title" => $t->getTag()->getTitle(),
But the weirdest thing is that it does not stop in the first entrance it shows the error when the index in the loop is equal 1.

How to reproduce

So if I run a test and do,

/**
     * @return Array
     */
    public function getTagsArray()
    {
        $tags = [];
        foreach ($this->tags as $t) {
              var_dump( $t->getTag()->getId()  );
        }
        return $tags;
   } 

that will print : int(1) int(2) int(3)
But if instead I run a test doing

/**
     * @return Array
     */
    public function getTagsArray()
    {
        $tags = [];
        foreach ($this->tags as $t) {
              var_dump( $t->getTag()->getId()  );
              var_dump( $t->getTag()->getTitle()  );
        }
        return $tags;
    }

that will print int(1) string(4) "Tag1" int(2)
As you can see and I explain before it stops the execution and through an error on the second Loading.

image

Expected behavior

Returning an array of multiple tags.

Originally created by @mmoradeveloper on GitHub (May 8, 2020). ### Bug Report PHP 7.4.5 Symfony 4.4 "symfony/orm-pack": "^1.0", "doctrine/doctrine-bundle": "v2.0.8", "doctrine/orm": "v2.7.2", #### Summary We generate manually a behaviour ManyToMany because we need the ability to generate more content into the middle class. So we have Product Related to Tags. and all we want is to get the list of tags from a call into ProductEntity we receive an Exception: "Notice: Undefined index: tag" We have the following Classes: ProductEntity ``` /** * @ORM\Table(name="product") * @ORM\Entity */ class ProductEntity { /** * @ORM\OneToMany(targetEntity="App\Entity\Product\DomainModel\ProductTagRelationEntity", mappedBy="product") * @EXCLUDE */ private $tags; /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * */ private $id; /** * @var string $title * * @ORM\Column(name="title", type="string", length=2555, nullable=true) */ private $title; public function __construct() { $this->tags = new ArrayCollection(); } /** * @return ArrayCollection */ public function getTags() { return $this->tags; } /** * @return Array */ public function getTagsArray() { $tags = []; foreach ($this->tags as $t) { $tags[] = [ "id" => $t->getTag()->getId(), "link_id" => $t->getId(), "title" => $t->getTag()->getTitle(), "type" => $t->getTag()->getType() ]; } return $tags; } } ``` ProductTagRelationEntity ``` /** * @ORM\Table(name="product_tag_relation") * @ORM\Entity * @ORM\Entity(repositoryClass="App\Repository\Product\Tag\TagRepository") */ class ProductTagRelationEntity{ /** * @ORM\ManyToOne(targetEntity="App\Entity\Product\DomainModel\ProductEntity" , inversedBy="tags") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="SET NULL") * }) * @EXCLUDE */ private $product; /** * @ORM\ManyToOne(targetEntity="App\Entity\Product\DomainModel\ProductTagEntity" , inversedBy="tags") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="SET NULL") * }) * @EXCLUDE */ private $tag; /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * */ private $id; /** * Get tag * * @return ProductTagEntity $tag */ public function getTag() { return $this->tag; } } ``` ` And the ProductTagEntity ` ``` /** * App\Entity\ProductTag\ProductTag * @ORM\Table(name="product_tag") * @ORM\Entity * @ORM\Entity(repositoryClass="App\Repository\ProductTag\ProductTagRepository") */ class ProductTagEntity { /** * @ORM\OneToMany(targetEntity="App\Entity\Product\DomainModel\ProductTagRelationEntity", mappedBy="tag") * @EXCLUDE */ private $tags; /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * */ private $id; /** * @var string $title * * @ORM\Column(name="title", type="string", length=2555, nullable=true) */ private $title; /** * @var string $slug * * @ORM\Column(name="slug", type="string", length=2555, nullable=true) */ private $slug; } ``` ` #### Current behavior So when we get a product from a repository call; `$product = $this->productRepository->getBySlug($slug);` and when we call the function `$productEntity->getTagsArray();` the run stops in the line where we call "title" => $t->getTag()->getTitle(), But the weirdest thing is that it does not stop in the first entrance it shows the error when the index in the loop is equal 1. #### How to reproduce So if I run a test and do, ```` /** * @return Array */ public function getTagsArray() { $tags = []; foreach ($this->tags as $t) { var_dump( $t->getTag()->getId() ); } return $tags; } ```` that will print : `int(1) int(2) int(3)` But if instead I run a test doing ```` /** * @return Array */ public function getTagsArray() { $tags = []; foreach ($this->tags as $t) { var_dump( $t->getTag()->getId() ); var_dump( $t->getTag()->getTitle() ); } return $tags; } ```` that will print `int(1) string(4) "Tag1" int(2)` As you can see and I explain before it stops the execution and through an error on the second Loading. ![image](https://user-images.githubusercontent.com/16003031/81400991-3d36ae80-9126-11ea-9cd1-cc0457ead816.png) #### Expected behavior Returning an array of multiple tags.
admin closed this issue 2026-01-22 15:33:38 +01:00
Author
Owner

@mmoradeveloper commented on GitHub (May 20, 2020):

A class was missing a OneToOne Relationship

@mmoradeveloper commented on GitHub (May 20, 2020): A class was missing a OneToOne Relationship
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6462