atlasphp / Atlas.Orm

A data mapper implementation for your persistence model in PHP.
http://atlasphp.io
MIT License
427 stars 23 forks source link

$atlas->select()->cols()->fetchRecord() throws exception when you don't include the primary key col #66

Closed jelofson closed 6 years ago

jelofson commented 7 years ago

The following will throw an exception because cols does not include post_id.

// Only want the title col
$post = $atlas->select(PostsMapper::class)
    ->where('post_id = ?', 3)
    ->cols(['title', 'summary'])
    ->fetchRecord();

You should be able to fetch rows without including the primary key. Is there another way to calculate the identity key for the map?

Stack:

Atlas\Orm\Exception: Expected scalar value for primary key 'post_id', got array instead. in /home/jelofson/www/atlas/vendor/atlas/orm/src/Exception.php:243
Stack trace:
#0 /home/jelofson/www/atlas/vendor/atlas/orm/src/Table/AbstractTable.php(700): Atlas\Orm\Exception::primaryValueNotScalar('post_id', Array)
#1 /home/jelofson/www/atlas/vendor/atlas/orm/src/Table/AbstractTable.php(560): Atlas\Orm\Table\AbstractTable->calcIdentity(Array)
#2 /home/jelofson/www/atlas/vendor/atlas/orm/src/Table/TableSelect.php(309): Atlas\Orm\Table\AbstractTable->getSelectedRow(Array)
#3 [internal function]: Atlas\Orm\Table\TableSelect->fetchRow()
#4 /home/jelofson/www/atlas/vendor/atlas/orm/src/Mapper/MapperSelect.php(98): call_user_func_array(Array, Array)
#5 /home/jelofson/www/atlas/vendor/atlas/orm/src/Mapper/MapperSelect.php(217): Atlas\Orm\Mapper\MapperSelect->__call('fetchRow', Array)
#6 /home/jelofson/www/atlas/src/Blog/Controllers/Posts.php(28): Atlas\Orm\Mapper\MapperSelect->fetchRecord()
...
pmjones commented 7 years ago

Do you actually want a full Record object here? The entirety of Aura.Sql ExtendedPdo fetch*() and yield*() methods is available; e.g., you can use fetchOne() to get back ['title' => ..., 'summary' => ...].

This explains a bit more: https://github.com/atlasphp/Atlas.Orm/blob/1.x/docs/direct.md

jelofson commented 7 years ago

I can definitely do that. No problem there. I just didn't expect to get an exception simply because I was calling fetchRecord but didn't have the primary key. If I did want a record, for example, if I wanted to call some Record methods, then I would just have to make sure the primary key is included.

Would you consider that something that will need addressing or something that should simply be documented?

harikt commented 7 years ago

I think documenting it may be good ( I guess it is a rare case ) . Else what will happen if someone tries to update the record ? It is partial data after all.

jelofson commented 6 years ago

I was going to close this, but I don't think I have written any docs about it yet. If it's no trouble, I will leave it open as a reminder to myself.

pmjones commented 6 years ago

I think the docs for this have been written and are live at http://atlasphp.io/mapper/2.x/reading.html#1-1-3-1

Thanks @jelofson for doing so!

jelofson commented 6 years ago

Oh man. Sorry for leaving that open for so long. Thanks for closing.

pmjones commented 6 years ago

Not at all, sir. :-)