Database\Selection: Remove \ArrayAccess implementation?

4 years ago

Svaťa Šimara
Member | 107


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


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, not a And what will do $this->rows[$primary] when one author has more books? $primary equals, 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:…se/issues/13#…