DDC-617: ObjectHydrator issue with OneToMany relationship #5126

Closed
doctrinebot opened this Issue May 30, 2010 · 6 comments

2 participants

@doctrinebot

Jira issue originally created by user numan:

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>
@doctrinebot

Comment created by numan:

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();
@doctrinebot

Comment created by romanb:

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

@doctrinebot

Comment created by @beberlei:

@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?

@doctrinebot

Comment created by @beberlei:

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

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.0.1 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment