gabordemooij / redbean

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

Have beans remember which database they come from #629

Closed Lynesth closed 6 years ago

Lynesth commented 6 years ago

So I'm currently working on a big project with many databases and I have to select the correct database each time I need to do something on a bean to make sure it's the correct one.

So I was thinking, why not have each bean remember which toolbox it needs to access once it's been dispensed ?

I'm currently doing it using a custom beanHelper that I set for each new database:

<?php
class IntelligentBeanHelper extends RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper {
    public function getToolbox() {
        return \RedBeanPHP\Facade::$toolboxes[$this->database];
    }

    public function getExtractedToolbox() {
        $toolbox = \RedBeanPHP\Facade::$toolboxes[$this->database];
        return array( $toolbox->getRedbean(), $toolbox->getDatabaseAdapter(), $toolbox->getWriter(), $toolbox );
    }

    public function __construct($database) {
        $this->database = $database;
    }
}

// Let's add some databases
R::addDatabase( 'db1', ... );
$bh = new IntelligentBeanHelper( 'db1' );
R::$toolboxes[ 'db1' ]->getRedBean()->setBeanHelper( $bh );

R::addDatabase( 'db2', ... );
$bh = new IntelligentBeanHelper( 'db2' );
R::$toolboxes[ 'db2' ]->getRedBean()->setBeanHelper( $bh );

R::addDatabase( 'db3', ... );
$bh = new IntelligentBeanHelper( 'db3' );
R::$toolboxes[ 'db3' ]->getRedBean()->setBeanHelper( $bh );

R::selectDatabase( 'db1' );
$book = R::load('book', 1);

R::selectDatabase( 'db2' );
$pizza = R::dispense('pizza');

R::selectDatabase( 'db3' );

// This works even with db3 currently selected
$book->xownPageList;

// This works too but need a very small change in the core of RedBean first
$pizza->pepperoni = true;
R::store($pizza);

// And so on....
?>

Is this something we'd be interesting in providing inside RedBean ? If so, should it be the default behavior ? Only as an option ?

Let me know what you think @gabordemooij and anyone else using RedBean regularly, and if you're interested I'll submit a PR.

Lyn.

gabordemooij commented 6 years ago

I like this approach very much, I think we should try to turn this into a plugin or something. I don't think it's wise to implement the bits of logic required to make this work throughout the entire codebase.

gabordemooij commented 6 years ago

I guess you need https://github.com/gabordemooij/redbean/pull/632 for this to work?

Lynesth commented 6 years ago

This currently cannot work for Facade static function (the OODB isn't accessed the same way).

And yes, I need #632 for this :)

gabordemooij commented 6 years ago

I am taking a shot at this as well...

Lynesth commented 6 years ago

Nice.

I thought I should take it step by step, so I'm actually making a PR (which I don't think you'll like much :p) regarding part of this issue.

gabordemooij commented 6 years ago

What about this:

https://gist.github.com/gabordemooij/6feefc4964cafa766a085557d8900389

It seems to work, and requires almost no changes in RedBeanPHP at all.

Lynesth commented 6 years ago

@gabordemooij Can I expect anything regarding this matter ? I understand that the way I proposed it (#634 / #641) surely wasn't how it should be done, but are you considering implementing this functionnality or not ?

I'm not being agressive or anything, just need to know so that I can really work on this on my own if that's the case :)

gabordemooij commented 6 years ago

I have created a plugin and waiting for your feedback...

Op di 12 jun. 2018 04:09 schreef Lynesth notifications@github.com:

@gabordemooij https://github.com/gabordemooij Can I expect anything regarding this matter ? I understand that the way I proposed it (#634 https://github.com/gabordemooij/redbean/pull/634 / #641 https://github.com/gabordemooij/redbean/pull/641) surely wasn't how it should be done, but are you considering implementing this functionnality or not ?

I'm not being agressive or anything, just need to know so that I can really work on this on my own if that's the case :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gabordemooij/redbean/issues/629#issuecomment-396442870, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFe4jW-zFSEOjU7uQAiqRNPPsiB5oP6ks5t7yL1gaJpZM4TgHqI .

Lynesth commented 6 years ago

I actually commented on this gist on the same day you posted it, I think you may have missed that :)

gabordemooij commented 6 years ago

Ok, I must have missed that, sorry. I will look into it asap.

Op di 12 jun. 2018 06:53 schreef Lynesth notifications@github.com:

I actually commented on this gist on the same day you posted it, I think you may have missed that :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gabordemooij/redbean/issues/629#issuecomment-396465184, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFe4gG-NU1r9dQJyIiuNMrvB4fXVEryks5t70ksgaJpZM4TgHqI .

gabordemooij commented 6 years ago

ping @Lynesth https://gist.github.com/gabordemooij/6feefc4964cafa766a085557d8900389

gabordemooij commented 6 years ago

I will continue to work on this as a plugin but it will not be a core 'feature' of RedBeanPHP.