Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:35:36+00:00 2026-06-16T17:35:36+00:00

I want to retrieve data from two different tables depending of the value of

  • 0

I want to retrieve data from two different tables depending of the value of one field (type). Basically if type is 2 or 3, relate ‘subcategory_id’ to category2 or category3 respectively

The plain SQL:

SELECT s.id, s.subcategory_id, s.type, IF(s.type = 2, c2.name, c3.name) as name
                                   FROM show_subcategory s
                                   LEFT JOIN category2 c2 ON (s.type = 2 AND s.subcategory_id = c2.id)
                                   LEFT JOIN category3 c3 ON (s.type = 3 AND s.subcategory_id = c3.id)
                                   WHERE s.category1_id = 1 ORDER BY s.order_list

The translation to DQL into Repository Class:

$em = $this->getEntityManager();
    $rsm = new ResultSetMapping;
    $rsm->addEntityResult('Acme\CoreBundle\Entity\ShowSubcategory', 's');
    $rsm->addFieldResult('s', 'id', 'id');
    $rsm->addFieldResult('s', 'subcategory_id', 'subcategoryId');
    $rsm->addFieldResult('s', 'type', 'type');
    $rsm->addFieldResult('s', 'order_list', 'orderList');

    $rsm->addJoinedEntityResult('Acme\CoreBundle\Entity\Category2' , 'c2', 's', 'category2');
    $rsm->addFieldResult('c2', 'id', 'id');
    $rsm->addFieldResult('c2', 'name', 'name');

    $rsm->addJoinedEntityResult('Acme\CoreBundle\Entity\Category3' , 'c3', 's', 'category3');
    $rsm->addFieldResult('c3', 'id', 'id');
    $rsm->addFieldResult('c3', 'name', 'name');

     $q = $em->createNativeQuery("SELECT s.id, s.subcategory_id, s.type, IF(s.type = 2, c2.name, c3.name) as name
                                   FROM show_subcategory s
                                   LEFT JOIN category2 c2 ON (s.type = 2 AND s.subcategory_id = c2.id)
                                   LEFT JOIN category3 c3 ON (s.type = 3 AND s.subcategory_id = c3.id)
                                   WHERE s.category1_id = ? ORDER BY s.order_list", $rsm);
    $q->setParameter(1, $category1);

    return $q->getResult();

In the entity for ShowSubcategory I added two new properties to handle with this query(category2, category3):

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

/**
 * @var integer $subcategoryId
 *
 * @ORM\Column(name="subcategory_id", type="integer", nullable=false)
 */
protected $subcategoryId;

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

/**
 * @var smallint $orderList
 *
 * @ORM\Column(name="order_list", type="smallint", nullable=true)
 */
protected $orderList;

/**
 * @var \Category1
 *
 * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Category1")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="category1_id", referencedColumnName="id")
 * })
 */
protected $category1;

/**
 * @var category2
 *
 * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Category2")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="subcategory_id", referencedColumnName="id")
 * })
 * @Assert\Blank()
 */
protected $category2;

/**
 * @var category3
 *
 * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Category3")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="subcategory_id", referencedColumnName="id")
 * })
 * @Assert\Blank()
 */
protected $category3;


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


/**
 * Set category1
 *
 * @param \Acme\CoreBundle\Entity\Category1 $category1
 * @return ShowSubcategory
 */
public function setCategory1(\Acme\CoreBundle\Entity\Category1 $category1 = null)
{
    $this->category1 = $category1;

    return $this;
}

/**
 * Get category1
 *
 * @return Acme\CoreBundle\Entity\Category1
 */
public function getCategory1()
{
    return $this->category1;
}

/**
 * Set subcategoryId
 *
 * @param integer $subcategoryId
 * @return ShowSubcategory
 */
public function setSubcategoryId($subcategoryId)
{
    $this->subcategoryId = $subcategoryId;

    return $this;
}

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

/**
 * Set type
 *
 * @param smallint $type
 * @return ShowSubcategory
 */
public function setType($type)
{
    $this->type = $type;

    return $this;
}

/**
 * Get type
 *
 * @return smallint
 */
public function getType()
{
    return $this->type;
}

/**
 * Set order
 *
 * @param smallint $OrderList
 * @return ShowSubcategory
 */
public function setOrderList($orderList)
{
    $this->orderList = $orderList;

    return $this;
}

/**
 * Get OrderList
 *
 * @return smallint
 */
public function getOrderList()
{
    return $this->orderList;
}

public function getCategory2()
{
    return $this->category2;
}

public function setCategory2(Category2 $category2 = null)
{
    $this->category2 = $category2;
    return $this;
}

public function getCategory3()
{
    return $this->category3;
}

public function setCategory3(Category3 $category3 = null)
{
    $this->category3 = $category3;
    return $this;
}

}

I get this Error:

Notice: Undefined index: id in /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2402 

I don’t know what is wrong I have tried several ideas, but nothing seems to work. Any suggestion?.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:35:37+00:00Added an answer on June 16, 2026 at 5:35 pm

    I guess you have to explicitely add id in the SELECT clause :

    SELECT s.id, s.subcategory_id, s.type, IF(s.type = 2, c2.name, c3.name) as name, c2.id as c2id, c3.id as c3id
        FROM show_subcategory s
        LEFT JOIN category2 c2 ON (s.type = 2 AND s.subcategory_id = c2.id)
        LEFT JOIN category3 c3 ON (s.type = 3 AND s.subcategory_id = c3.id)
        WHERE s.category1_id = ? ORDER BY s.order_list
    

    And adapt the ResultSet as following :

    $rsm = new ResultSetMapping;
    $rsm->addEntityResult('Acme\CoreBundle\Entity\ShowSubcategory', 's');
    $rsm->addFieldResult('s', 'id', 'id');
    $rsm->addFieldResult('s', 'subcategory_id', 'subcategoryId');
    $rsm->addFieldResult('s', 'type', 'type');
    $rsm->addFieldResult('s', 'order_list', 'orderList');
    
    $rsm->addJoinedEntityResult('Acme\CoreBundle\Entity\Category2' , 'c2', 's', 'category2');
    $rsm->addFieldResult('c2', 'c2id', 'id');
    $rsm->addFieldResult('c2', 'name', 'name');
    
    $rsm->addJoinedEntityResult('Acme\CoreBundle\Entity\Category3' , 'c3', 's', 'category3');
    $rsm->addFieldResult('c3', 'c3id', 'id');
    $rsm->addFieldResult('c3', 'name', 'name');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two XML sources to retrieve data from. I want to use them
i have records in two tables, and 1 object, i want to retrieve data
I'm using the following code in an attempt to retrieve data from two tables
I have two different servers, and I need to send post data from one
I want to retrieve two different fields residing in different data bases. My db
I have DropDownList with CascadingDropDown extender. If I want to retrieve data from the
I want applications to share certain database data. I want one application to retrieve
I have a database where I store two different kinds of data. One table
I want to retrieve the data from MS.Access through list but when run the
I have two tables users registered_members I want to confirm values from user table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.