Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-617

ObjectHydrator issue with OneToMany relationship

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-BETA1
    • Fix Version/s: 2.0.1, 2.1
    • Component/s: None
    • Security Level: All
    • Labels:
      None
    • Environment:
      XAMPP 1.7.3 on Windows Vista

      Description

      I'm pretty new to this ORM thing but I am having a weird issue with a DQL query I think should work (or throw an exception). I have the following code:

      namespace Lms\Entities;
      /**
       *
       * @Entity
       * @Table(name="User")
       */
      class User {
      
      	public function __constructor() {
      		$social_media_links = new \Doctrine\Common\Collections\ArrayCollection;
      	}
      	/**
      	 * @Id
      	 * @Column(type="bigint")
      	 * @GeneratedValue
      	 */
      	protected $id;
      
      	/** @Column(type="string", nullable=TRUE, length=1024) */
      	protected $mission_statement;
      
      	/** @Column(type="string") */
      	protected $first_name;
      
      	/** @Column(type="string") */
      	protected $last_name;
      
      	/** @Column(type="string", nullable=TRUE, length=8) */
      	protected $initial;
      
      	/** @Column(type="string", nullable=TRUE) */
      	protected $email;
      
      	/** @Column(type="string", nullable=TRUE) */
      	protected $company;
      
      	/** @Column(type="string", nullable=TRUE, length=1024) */
      	protected $picture;
      
      	/** @Column(type="text", nullable=TRUE) */
      	protected $bio;
      
      	// unidirectional one to many relationship with social media link
      	/**
      	 * @ManyToMany(targetEntity="SocialMediaLink")
      	 * @JoinTable(name="Users_Socialmedialinks",
      	 *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
      	 *      inverseJoinColumns={@JoinColumn(name="socialmedialink_id", referencedColumnName="id", unique=true)}
      	 *      )
      	 */
      	protected $social_media_links;
      	
      
          /**
           * Add social_media_links
           *
           * @param SocialMediaLink $socialMediaLinks
           */
          public function addSocialMediaLinks(\SocialMediaLink $socialMediaLinks)
          {
              $this->social_media_links[] = $socialMediaLinks;
          }
      
          /**
           * Get social_media_links
           *
           * @return Doctrine\Common\Collections\Collection $socialMediaLinks
           */
          public function getSocialMediaLinks()
          {
              return $this->social_media_links;
          }
      }
      
      namespace Lms\Entities;
      
      /**
       *
       * @Entity
       * @Table(name="SocialMediaLink")
       */
      class SocialMediaLink {
      
      	/**
      	 * @Id
      	 * @Column(type="bigint")
      	 * @GeneratedValue
      	 */
      	protected $id;
      
      	/** @Column(type="string") */
      	protected $title;
      
      	/** @Column(type="string") */
      	protected $link;
      
      }
      

      And when I execute:

      		$sml = $this->_em->createQuery("SELECT s FROM Lms\Entities\User u JOIN u.social_media_links  AS s WHERE u.id = ?1 AND s.id = ?2")
      					->setParameter(1, 1)
      					->setParameter(2, 1)
      					->getResult();
      

      I get the following trace:

      1) DoctrineExtensions\PHPUnit\UserTestCase::testEditSocialMediaLink
      class_parents(): object or string expected
      
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php:201
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php:224
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php:148
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\EntityManager.php:235
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php:220
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php:75
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\Internal\Hydration\AbstractHydrator.php:98
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\AbstractQuery.php:530
      C:\xampp\htdocs\lms-user-sml-feature\system\application\libraries\Doctrine\ORM\AbstractQuery.php:360
      

      The following data is in the database:

      <?xml version="1.0" encoding="UTF-8"?>
      <dataset>
      	<table name="user">
      		<column>id</column>
      		<column>mission_Statement</column>
      		<column>first_name</column>
      		<column>last_name</column>
      		<column>initial</column>
      		<column>email</column>
      		<column>company</column>
      		<column>picture</column>
      		<column>bio</column>
      		
      		<row>
      			<value>1</value>
      			<value>Mission Statement 1</value>
      			<value>First Name 1</value>
      			<value>Last Name 1</value>
      			<value>IN1</value>
      			<value>email1@email.com</value>
      			<value>Company 1</value>
      			<value>Picture 1</value>
      			<value>Bio 1</value>
      		</row>
      	</table>
      	
      	<table name="socialmedialink">
      		<column>id</column>
      		<column>title</column>
      		<column>link</column>
      
      		<row>
      			<value>1</value>
      			<value>Social Media Title 1</value>
      			<value>Social Media Link 1</value>
      		</row>
      	</table>
      	
      	<table name="users_socialmedialinks">
      		<column>user_id</column>
      		<column>socialmedialink_id</column>
      
      		<row>
      			<value>1</value>
      			<value>1</value>
      		</row>
      	</table>
      </dataset>
      
      1. SocialMediaLink.php
        1 kB
        Numan S
      2. User.php
        7 kB
        Numan S

        Activity

        Hide
        Numan S added a comment -

        After messing around with it a little bit, the following will work:

        	
        		$sml = $this->_em->createQuery("SELECT u,s FROM Lms\Entities\User u JOIN u.social_media_links AS s WHERE u.id = ?1 AND s.id = ?2")
        					->setParameter(1, 1)
        					->setParameter(2, 1)
        					->getResult();
        
        Show
        Numan S added a comment - After messing around with it a little bit, the following will work: $sml = $ this ->_em->createQuery( "SELECT u,s FROM Lms\Entities\User u JOIN u.social_media_links AS s WHERE u.id = ?1 AND s.id = ?2" ) ->setParameter(1, 1) ->setParameter(2, 1) ->getResult();
        Hide
        Roman S. Borschel added a comment -

        Hydration does not (yet?) support such kinds of queries where the root entity/schema name is not selected.

        Show
        Roman S. Borschel added a comment - Hydration does not (yet?) support such kinds of queries where the root entity/schema name is not selected.
        Hide
        Benjamin Eberlei added a comment -

        @roman i think this should be solved in conjunction with DDC-736 and throw an exception if the users tries to do this. Is this possible in linear time?

        Show
        Benjamin Eberlei added a comment - @roman i think this should be solved in conjunction with DDC-736 and throw an exception if the users tries to do this. Is this possible in linear time?
        Hide
        Benjamin Eberlei added a comment -

        Fixed, a QueryException will now be thrown if attempting to select one or more identification variables without picking at least one root.

        Show
        Benjamin Eberlei added a comment - Fixed, a QueryException will now be thrown if attempting to select one or more identification variables without picking at least one root.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Numan S
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: