Skip to content

Instantly share code, notes, and snippets.

@dominics
Last active February 28, 2017 01:10
Show Gist options
  • Save dominics/58b423df4458eef0a99f8d88c36f03bc to your computer and use it in GitHub Desktop.
Save dominics/58b423df4458eef0a99f8d88c36f03bc to your computer and use it in GitHub Desktop.
Redbean issue #544
<?php
require_once __DIR__ . '/vendor/autoload.php';
use RedBeanPHP\Facade as R;
// Start setup
R::setup(getenv('REDBEAN_DSN'), getenv('REDBEAN_USERNAME'), getenv('REDBEAN_PASSWORD'));
R::exec('DROP TABLE child');
R::exec('DROP TABLE parent');
$parent = R::dispense('parent');
$parent->name = 'James';
R::store($parent);
$child = R::dispense('child');
$child->name = 'Jack';
$child->parent = $parent;
R::store($child);
$child = R::dispense('child');
$child->name = 'Jill';
$child->parent = $parent;
R::store($child);
// End setup
// Lets begin with a fresh instance, retrieved from the DB
$retrieved = R::findOne('child', 'name = ?', ['Jack']);
// We can create a fresh instance, and inject into it without errors
$imported = R::dispense('child');
$imported->inject($retrieved);
// The record has no 'parent' property, as expected, because we haven't lazy-loaded it yet
var_dump($retrieved->getProperties()['parent_id']); // 1
var_dump($retrieved->getProperties()['parent'] ?? null); // null
// Now we lazy load it
$retrieved->parent;
// And now we see it on the record
var_dump($retrieved->getProperties()['parent_id']); // 1
var_dump($retrieved->getProperties()['parent']); // OODBBean parent object
// But now injecting into a fresh instance no longer works
$imported = R::dispense('child');
$imported->inject($retrieved);
/*
Warning: Illegal string offset '_type' in /vagrant/redbug/vendor/gabordemooij/redbean/RedBeanPHP/OODBBean.php on line 487
Call Stack:
0.1119 347776 1. {main}() /vagrant/redbug/test.php:0
0.1821 630128 2. RedBeanPHP\OODBBean->inject(???) /vagrant/redbug/test.php:48
0.1821 630880 3. RedBeanPHP\OODBBean->import(???, ???, ???) /vagrant/redbug/vendor/gabordemooij/redbean/RedBeanPHP/OODBBean.php:551
Fatal error: Uncaught Error: Cannot unset string offsets in /vagrant/redbug/vendor/gabordemooij/redbean/RedBeanPHP/OODBBean.php on line 488
Error: Cannot unset string offsets in /vagrant/redbug/vendor/gabordemooij/redbean/RedBeanPHP/OODBBean.php on line 488
Call Stack:
0.1119 347776 1. {main}() /vagrant/redbug/test.php:0
0.1821 630128 2. RedBeanPHP\OODBBean->inject(???) /vagrant/redbug/test.php:48
0.1821 630880 3. RedBeanPHP\OODBBean->import(???, ???, ???) /vagrant/redbug/vendor/gabordemooij/redbean/RedBeanPHP/OODBBean.php:551
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment