For example, take these four tables:

    A
    - id

    B
    - id

    C
    - id

    Connector
    - a_id
    - b_id
    - c_id


pretty straightforward.
The table Connector has a many-to-one connection to each of the tables.

For testing-purposes, the database contains following values:

The tables *A*,*B*,*C* each contain 3 rows with *1*, *2* and *3* as the 
values for the id.

The table *Connector* connects those with the same value:

----------------------
| a_id | b_id | c_id |
----------------------
|    1 |    1 |    1 |
|    2 |    2 |    2 |
|    3 |    3 |    3 |
----------------------


Now lets query them with DQL:

    $query = $entityManager->createQuery(
        'SELECT connector, c
        FROM \ORM\Connector connector
        INNER JOIN connector.c c
    ');
    $results = $query->getResult();


The result is a Collection of objects. 
I print each id with *$result->getC()->getId()*:


    A ID: 1; B ID: 1; C ID: 2;
    A ID: 2; B ID: 2; C ID: 3;
    A ID: 3; B ID: 3; C ID: null;


Uhm, okay, not what I was expecting.
Doctrine seems to connect the rows of different tables incorrectly.

I left `A` and `B` intentionally out of the query; This way Doctrine 
accesses the `a_id` and `b_id` columns of `Connector` directly, which makes 
the offset of the internal objects visible.

If I fetch the tables a and b in the DQL-Query, too, the result would look 
like this:

    A ID: 1; B ID: 1; C ID: 2;
    A ID: 2; B ID: 2; C ID: 3;
    A ID: null; B ID: null; C ID: null;


Is this known behaviour or possibly a bug?

I could not find any information on this, help would be appreciated.

- Pascal



-------------------------------------------

Some code for completeness:

The model for the tables A/B/C looks like this:

Babesk\ORM\A:
    type: entity
    table: A
    id:
        id:
            type: integer
            id: true
    oneToMany:
        connector:
            targetEntity: Babesk\ORM\Connector
            mappedBy: a



The model for the Connector:

Babesk\ORM\Connector:
    type: entity
    table: Connector
    id:
        a:
            associationKey: true
        b:
            associationKey: true
        c:
            associationKey: true
    manyToOne:
        a:
            targetEntity: Babesk\ORM\A
            inversedBy: connector
        b:
            targetEntity: Babesk\ORM\B
            inversedBy: connector
        c:
            targetEntity: Babesk\ORM\C
            inversedBy: connector



The PHP-file to test it:

// ...init entityManager...


require_once 'models/Entities/Babesk/ORM/A.php';
require_once 'models/Entities/Babesk/ORM/B.php';
require_once 'models/Entities/Babesk/ORM/C.php';
require_once 'models/Entities/Babesk/ORM/Connector.php';

// Only fetching C
$query = $entityManager->createQuery(
    'SELECT connector, a, b, c
    FROM \Babesk\ORM\Connector connector
    INNER JOIN connector.a a
    INNER JOIN connector.b b
    INNER JOIN connector.c c
');
$results = $query->getResult();

foreach($results as $connector) {
    $c = $connector->getC();
    // If association-object exists, output the values
    if(isset($c) || true) {
        // lazy-load A and B to show the displacement-effect.
        //
        // If the DQL-Query would fetch all elements, the ids
        // would be correct for all but the last entry (Where one
        // of the ids would be null).
        // I guess this is because the ids then get loaded directly
        // from A/B/C instead of Connector itself.
        echo('A ID: ' . $connector->getA()->getId() . '; ');
        echo('B ID: ' . $connector->getB()->getId() . '; ');
        echo('C ID: ' . $c->getId() . '; ');
        echo('<br>');

        // Output:
        //
        // A ID: 1; B ID: 1; C ID: 2;
        // A ID: 2; B ID: 2; C ID: 3;
        // Null!
        // Finished

        // Compare that to the Database-content:
        // INSERT INTO A (id) VALUES (1), (2), (3);
        // INSERT INTO B (id) VALUES (1), (2), (3);
        // INSERT INTO C (id) VALUES (1), (2), (3);
        // INSERT INTO Connector (aId, bId, cId)
        //    VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3);
    }
    else {
        echo('Null!<br>');
    }
}
die('Finished');


And the SQL-File to setup the test-database:

CREATE TABLE `A` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `B` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `C` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `Connector` ( -- uigs
  `a_id` int(11) NOT NULL,
  `b_id` int(11) NOT NULL,
  `c_id` int(11) NOT NULL,
  PRIMARY KEY (`a_id`,`b_id`,`c_id`)
) ENGINE=InnoDB;

INSERT INTO A (id) VALUES (1), (2), (3);
INSERT INTO B (id) VALUES (1), (2), (3);
INSERT INTO C (id) VALUES (1), (2), (3);
INSERT INTO Connector (a_id, b_id, c_id)
    VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3);



-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to