j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

join() returns the id field of the joined table, not the forTable(xxx) table #285

Closed thepurpleblob closed 8 years ago

thepurpleblob commented 8 years ago

If two tables (as is likely) each have an id field then the join seems to return the value of the "joined" table. It seems logical to me that it would return the id field from the table specified in forTable(). This may be intentional but it isn't clear in the documentation. This is at odds with what happens if I construct the SQL query by hand.

$pricebands = \ORM::forTable('Priceband')->join('Destination', array('Priceband.destinationid', '=', 'Destination.id'))->where('pricebandgroupid', $id)->findMany();

(Priceband and Destination tables both have 'id' fields)

...this returns data containing the id field from the Destination table.

select * from priceband join destination on (priceband.destinationid = destination.id) where pricebandgroupid=52;

(which is what I would expect this constructs) returns the id fields from the Priceband table.

thepurpleblob commented 8 years ago

Just released you can add a select (..) in there to override the default but it's not clear from the documentation. The join documentation is a bit sparse.

treffynnon commented 8 years ago

Alias the fields as you would in straight SQL. This isn't the concern of the ORM, but a direct result of the query that you're running as it comes back via SQL and PHP - fields of the same name? Last one wins. On 14 Dec 2015 08:34, "Howard Miller" notifications@github.com wrote:

If two tables (as is likely) each have an id field then the join seems to return the value of the "joined" table. It seems logical to me that it would return the id field from the table specified in forTable(). This may be intentional but it isn't clear in the documentation. This is at odds with what happens if I construct the SQL query by hand.

$pricebands = \ORM::forTable('Priceband')->join('Destination', array('Priceband.destinationid', '=', 'Destination.id'))->where('pricebandgroupid', $id)->findMany();

(Priceband and Destination tables both have 'id' fields)

...this returns data containing the id field from the Destination table.

select * from priceband left join destination on (priceband.destinationid = destination.id) where pricebandgroupid=52;

(which is what I would expect this constructs) returns the id fields from the Priceband table.

— Reply to this email directly or view it on GitHub https://github.com/j4mie/idiorm/issues/285.

treffynnon commented 8 years ago

You can issue a pull request against the documentation - look forward to seeing your improvements. On 14 Dec 2015 08:43, "Howard Miller" notifications@github.com wrote:

Just released you can add a select (..) in there to override the default but it's not clear from the documentation. The join documentation is a bit sparse.

— Reply to this email directly or view it on GitHub https://github.com/j4mie/idiorm/issues/285#issuecomment-164307245.