GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing redbeanphp.com — RedBeanPHP :: Models
ModelsA model is a place to put validation and business logic. Imagine a Jazz band that can only have up to 4 members. We could implement this rule like this: However, now we need to add this check everytime we call R::store(). It would be much more convenient if R::store() was smart enough to perform this check by itself. We can accomplish this by putting the validation rule in our model. RedBeanPHP automatically discovers the models that belong to beans, so we can implement this validation like this:
RedBeanPHP automatically connects beans with models using a naming convention
(i.e. Model_{TYPE OF BEAN}).
R::store() will not invoke FUSE-methods if nothing has changed. In fact, store() will do nothing but return the ID if no changes have to be processed. If you like your models to reside in the namespace \Model, you can set the following constant: You can now create a model class like this: If you prefer no namespacing at all: Beans of types like 'book_page' will search for a model 'BookPage' first and if no such model is found they will try to connect to 'Book_Page'. As of RedBeanPHP 5.7.2 you can also use different namespaces per database: Scoping rulesWithin the model, the $this->bean variable refers to the bean. Simply $this also refers to the bean but without returning references, in practice this can be very confusing so I recommend to use $this->bean. Fused methods
Besides update() RedBeanPHP FUSE calls other methods on the model as well:
R::store() invokes update() and after_update(), Note that since loading a bean also causes a new bean to be dispensed to receive the record from the database, load also invokes dispense(). Example: all fused methodsTo demonstrate the order and use of all of these methods let's consider an example: output: Custom FUSED methodsBesides the standard methods mentioned above, any method on the model can be invoked by calling it on the bean (assuming it does not collide with a native bean method): If you call a method on a bean that does not exist in the bean and also not in the model the call will be ignored. You change this behaviour by selecting a different FUSE error handling mechanism using the setErrorHandlingFUSE() method, see API. Boxing and UnboxingIf you have a bean and you want to obtain the corresponding model use: Similarly, if you have a model and you want its inner bean, call: We call this technique boxing (and unboxing). This can be handy if you want to make use of typehinting: Otherwise, we would have to use type RedBean_OODBBean which is less descriptive. Model Factory and Dependency InjectionIf for some reason you need to control how the bean turns into a model you can pass a factory function like this: In this example we inject a mail library in a model using the factory function. For more complex scenarios you can even use the factory to pass the model to your own dependency injection framework.
If you need even more flexibility you can subclass the
SimpleFacadeBeanHelper
and override the getModelForBean() method.
As of RedBeanPHP 5.2 models can also return jsonSerialized objects by implementing the __jsonSerialize method (will override the default OODB implementation. As of RedBeanPHP 5.7.2 you can also use the TypedModel instead of the SimpleModel to make use of PHP 8 type hinting features: back to main menu
Donate to RedBeanPHP using Monero: RedBeanPHP Easy ORM for PHP © 2026 Gabor de Mooij
() and the RedBeanPHP community
(credits) - Licensed New BSD/GPLv2 -
RedBeanPHP Archives
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |