Closed fabswt closed 4 years ago
Thank you, looks good, I will review it and make it available on the website!
Any chance we can have this upgrade guide as a PDF maybe?
You can use pandoc to convert markdown to pdf.
Attaching a quick export via MacDown. Would this work?
Works like a charm! https://redbeanphp.com/index.php?p=/download#rb3to4 Thanks!
After many years of dedicated services, I finally upgraded from RedBean 3 to RedBean 4. I know… it's a bit late :-) It wasn't crazy complicated, but a few things did prove tricky (and I noticed a couple of mistakes.) I feel an upgrade guide would have helped, so I compiled one:
REDBEAN 4 UPGRADE GUIDE
This describes how to upgrade from RedBean 3.5 to RedBean 4.3.4.
Installing Redbean 4
Make sure to remove / stop including the old v3 library.
Then decide if you want to include RedBean 4 manually or via Composer.
Via Composer
composer require gabordemooij/redbean ^4
use \RedBeanPHP\R as R;
at the top of your app.class_alias('\RedBeanPHP\R','\R');
Uncaught Error: Class 'R' not found
errors.Note about namespaces: RedBean's default namespace is
\RedBeanPHP\R
when installed via Composer;\R
when installed manually (rb.php).If you're including RedBean via Composer and want to use the short form shown throughout the docs, then you need to use
use
orclass_alias
as shown above. Otherwise, the alternative is to use the fully qualified class wherever you use RedBean.(If including RedBean manually, do NOT use this
use
/alias
.)Manually
rb.php
.Preloader
If you were using the Preloader in RedBean 3.5, then install the preloader extension for RedBean 4, available from https://github.com/gabordemooij/RB4Plugins
Note: if it's missing but you use
R::preload
anyway, then you'll get a misleading error message:Plugin 'preload' does not exist, add this plugin using: R::ext('preload')
. AddingR::ext('preload')
won't help (indeed, the example that comes with Preloader.php does not use it); adding the plugin will.Configuring RedBean
Decide if you want to define your RedBean models with or without a namespace, and choose the prefix used for your models (both are connected.) Some examples:
Default prefix / No namespace:
With namespace and relevant prefix:
Note: the readme mentions
class \Model\Band extends \RedBeanPHP\SimpleModel
which is inaccurate and will cause "Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting identifier (T_STRING)".Without any prefix:
New to namespaces?
If you're now using a namespace for your models while in the past it did not, then you also need to either:
use
. e.g.:use \Datetime;
use \R;
Other things worth knowing
SQLHelper
The SQLHelper (that was accessed via
R::$f
in RedBean 3) is gone. Either:R::getAll()
;Namespace and models
If getting confused about namespaces and as to whether or not your model is being loaded,
class_exists
will come in handy, e.g.:var_dump(class_exists('Model_Book'));
var_dump(class_exists('Model\Book'));
var_dump(class_exists('Book'));
Debugging the FUSE model
In development, I recommend turning on error messages to know if a FUSE model method does not exist. Easy to do with:
R::setErrorHandlingFUSE(RedBeanPHP\OODBBean::C_ERR_EXCEPTION);
(See the API for the other possible error levels.)
Note:
setErrorHandlingFUSE
may throw misleading exceptions on foreign keys for lack of preload. In other words: if RedBean failed to preload foreign keyfoo
, it may throw:An exception has been thrown during the rendering of a template ("FUSE: method does not exist in model: foo")
… As iffoo
was a method, even though it's actually a foreign key. (Had this occur when trying to load a foreign key from within a Twig template without preloading; with preloading, no issue.)See also
Thank you for a great library!