addJoinedEntityResult does not seem to work with oneToMany/manyToOne #5799

Open
opened 2026-01-22 15:18:15 +01:00 by admin · 0 comments
Owner

Originally created by @yurtesen on GitHub (Dec 10, 2017).

Originally assigned to: @yurtesen on GitHub.

I am using symfony 3.4.1, with doctrine/orm 2.5.13. I have 2 tables. store and store_product.

In Store entity I have:

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Store\Product", mappedBy="store")
 * @ORM\JoinColumn(name="store_id")
 */
private $products;

and in Store\Product entity I have composite index with (store_id,product_id). I have:

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Store", inversedBy="products")
 * @ORM\JoinColumn(name="store_id",referencedColumnName="id",nullable=false,onDelete="CASCADE")
 * @ORM\Id()
 * @ORM\GeneratedValue("NONE")
 * @var $store \AppBundle\Entity\Store
 */
protected $store;

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product", inversedBy="stores")
 * @ORM\JoinColumn(name="product_id",referencedColumnName="id",nullable=false,onDelete="CASCADE")
 * @ORM\Id()
 * @ORM\GeneratedValue("NONE")
 * @var $product \AppBundle\Entity\Product
 */
protected $product;

Earlier I produced a native query using the guide. It was only fetching entries from store table and it worked perfectly.

Now I am trying to join store_product table and it is not going so well. I am using the following query which returns 1 result.

SELECT st.id, st.name, stp.store_id, stp.product_id stp.price FROM store st LEFT JOIN store_product stp ON st.id = stp.store_id WHERE st.id=1 LIMIT 1;

returns something like:

 id |    name    | store_id | product_id | price 
----+------------+----------+------------+-------
  1 | Store Name |        1 | 1234567890 |   129

I setup the result set mapping as follows:

    $rsm = new ResultSetMapping();
            $rsm->addEntityResult('AppBundle\Entity\Store', 'st');
            $rsm->addFieldResult('st', 'id', 'id');
            $rsm->addFieldResult('st', 'name', 'name');
            $rsm->addJoinedEntityResult('AppBundle\Entity\Store\Product', 'stp',
    'st', 'products');
            $rsm->addFieldResult('stp','store_id', 'store');
            $rsm->addFieldResult('stp','product_id','product');
            $rsm->addFieldResult('stp','price','price');

I am getting error: Notice: Undefined index: store Can anybody see the reason of the error?

Originally created by @yurtesen on GitHub (Dec 10, 2017). Originally assigned to: @yurtesen on GitHub. I am using symfony 3.4.1, with doctrine/orm 2.5.13. I have 2 tables. `store` and `store_product`. In `Store` entity I have: /** * @ORM\OneToMany(targetEntity="AppBundle\Entity\Store\Product", mappedBy="store") * @ORM\JoinColumn(name="store_id") */ private $products; and in `Store\Product` entity I have composite index with (store_id,product_id). I have: /** * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Store", inversedBy="products") * @ORM\JoinColumn(name="store_id",referencedColumnName="id",nullable=false,onDelete="CASCADE") * @ORM\Id() * @ORM\GeneratedValue("NONE") * @var $store \AppBundle\Entity\Store */ protected $store; /** * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product", inversedBy="stores") * @ORM\JoinColumn(name="product_id",referencedColumnName="id",nullable=false,onDelete="CASCADE") * @ORM\Id() * @ORM\GeneratedValue("NONE") * @var $product \AppBundle\Entity\Product */ protected $product; Earlier I produced a native query using the [guide][1]. It was only fetching entries from `store` table and it worked perfectly. Now I am trying to join `store_product` table and it is not going so well. I am using the following query which returns 1 result. `SELECT st.id, st.name, stp.store_id, stp.product_id stp.price FROM store st LEFT JOIN store_product stp ON st.id = stp.store_id WHERE st.id=1 LIMIT 1;` returns something like: id | name | store_id | product_id | price ----+------------+----------+------------+------- 1 | Store Name | 1 | 1234567890 | 129 I setup the result set mapping as follows: $rsm = new ResultSetMapping(); $rsm->addEntityResult('AppBundle\Entity\Store', 'st'); $rsm->addFieldResult('st', 'id', 'id'); $rsm->addFieldResult('st', 'name', 'name'); $rsm->addJoinedEntityResult('AppBundle\Entity\Store\Product', 'stp', 'st', 'products'); $rsm->addFieldResult('stp','store_id', 'store'); $rsm->addFieldResult('stp','product_id','product'); $rsm->addFieldResult('stp','price','price'); I am getting error: `Notice: Undefined index: store` Can anybody see the reason of the error? [1]: http://docs.doctrine-project.org/en/latest/reference/native-sql.html
admin added the BugMissing Tests labels 2026-01-22 15:18:15 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5799