gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 280 forks source link

Exporting a bean that is stored has different results than one that is not stored #947

Open krisives opened 1 month ago

krisives commented 1 month ago

When exporting a bean currently it will export different information based on if the bean has been stored or not.

Firstly, it's understood that the id column of a bean that has not yet been stored will be zero. This issue is not about that.

R::setup("sqlite:./test.db");

$author = R::dispense('author');
$author->name = 'Some Guy';

$book = R::dispense('book');
$book->title = 'Every Book Makes Someone Mad';
$book->author = $author;

// R::store($book);

var_dump($book->export());

The above code (without storing the bean) will output:

array(3) {
  ["id"]=>
  int(0)
  ["title"]=>
  string(28) "Every Book Makes Someone Mad"
  ["author"]=>
  array(2) {
    ["id"]=>
    int(0)
    ["name"]=>
    string(8) "Some Guy"
  }
}

However, if you uncomment that line and actually store the bean, you will get a different output:

array(3) {
  ["id"]=>
  string(1) "1"
  ["title"]=>
  string(28) "Every Book Makes Someone Mad"
  ["author_id"]=>
  string(1) "1"
}

Lastly, if we change the export() invocation to set $onlyMe = true and still export an unstored bean, like this:

R::setup("sqlite:./test.db");

$author = R::dispense('author');
$author->name = 'Some Guy';

$book = R::dispense('book');
$book->title = 'Every Book Makes Someone Mad';
$book->author = $author;

// R::store($book);

var_dump($book->export(false, false, true));

We still get an export that contains the author bean, despite the $onlyMe argument somewhat indicating this is not expected behavior.

Let me know if I can be of any help!

Lynesth commented 1 month ago

https://github.com/gabordemooij/redbean/issues/739#issuecomment-516360082