Database\Selection: Remove \ArrayAccess implementation?

4 years ago

Svaťa Šimara
Member | 107
+
0
-

Hi,

in my opinion \ArrayAccess implementation complicates using Selection. Especially this code:

src/Database/Table/Selection.php:504–509

foreach ($result->getPdoStatement() as $key => $row) {
    $row = $this->createRow($result->normalizeRow($row));
    $primary = $row->getSignature(FALSE);
    $usedPrimary = $usedPrimary && $primary;
    $this->rows[$primary ?: $key] = $row;
}

When primary key wasn't selected in ->select(), everything is ok. $this->rows[$key] is applied and $key is normal counter.

When primary key was selected in ->select(), everythis is also ok. $this->rows[$primary] is applied and $primary is a correct value.

But when you are selecting from table book, and both tables author and book has primary key id, that is normal for me. And you select books in this way:

$books = $context->table('book')->select('book.title, author.*');

What will happen? Selection will think that primary key was selected – id, but it is an author.id, not a book.id. And what will do $this->rows[$primary] when one author has more books? $primary equals author.id, so every author will have only one book in the final selection (or any if didn't write a book).

This is not what do I expect :-)

Solution: remove \ArrayAccess implementation, and simplify code to:

foreach ($result->getPdoStatement() as $row) {
    $this->rows[] = $this->createRow($result->normalizeRow($row));
}

Related problem: https://github.com/…se/issues/13#…