Export hook #408

Closed
Phyks opened this Issue Dec 24, 2014 · 5 comments

Projects

None yet

2 participants

@Phyks
Phyks commented Dec 24, 2014

Hi,

I need to store some arrays as plaintext in a database. I have to store an array in database, as it seems the best idea to me (it's basically some parameters to send with an URL request).

For now, I'm using FUSE and update method to serialize my arrays with json_encode and json_decode, on the fly, before storing them in the database. It's seems to work nicely, although it may not be the best method.

However, I can't export my bean, because this results in a function call with an array parameter, and it fails:

Fatal error: Call to a member function export() on array in /srv/http/Freeder/lib/Redbean/rb.php on line 1436

What about adding an export hook so that such weird datatypes could be handled by the Model before storage and export ?

Thanks

P.S.: Perhaps I misunderstood the idea behind Redbean models. In fact, I'd like to have my model automatically handling serialization and unserialization of complex objects, and not having to take care of this myself, when feeding the model. :)

@gabordemooij
Owner

It should be possible. Maybe you do something wrong? Can you send me a small piece of code without framework glue etc, pure bean and model, that demonstrates this issue? Then I will have a look...

@Phyks
Phyks commented Dec 25, 2014
class Feed extends \RedbeanPHP\SimpleModel {
    public function update() {
        $this->bean->post = json_encode($this->bean->post);
    }

    public function open() {
        $this->bean->post = json_decode($this->bean->post, true);
    }
}

This is to store some RSS feeds. post is a JSON encoded array of POST params to send when querying the feed, for auth and so. I'd like to have them stored as text in the database (JSON for ease, but could also be plain urlencode string) but to have access to them as an array in the bean itself. Then, I put the update and open functions so that the translation is automatically done without intervention, upon database operation.

Then, when I want to pretty print it, I use the following snippet.

    $feeds = R::findAll('feed');
    var_dump(R::exportAll($feeds));

I also tried using $feeds[1]->export() with the same result.

However, if I don't tweak my bean model to handle arrays, it works nicely.

Feel free to ask me if you need more infos.

Thanks

EDIT: I had a deeper look and this is due to the function member export being called recursively on the bean member $b on line 1436 of file rb.php which in my case is actually an array (as it is stored as an array in the bean, and automatically serialized upon database operation). Thus, this causes an error. I could manually write an export or pretty print function, but I'd rather not reinvent the wheel and use the export function if possible. Moreover, a hook for the export function (same as update, but called before export) could be used also for wider application, especially for export filtering, to prevent exporting some useless and application internal specific fields.

@gabordemooij
Owner

I think I see the issue, I will investigate this.

@gabordemooij gabordemooij added the BUG label Dec 26, 2014
@Phyks
Phyks commented Dec 26, 2014

Thanks !

@Phyks
Phyks commented Jan 2, 2015

I confirm the fix, thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment