RedBeanPHP
The Power ORM

Many-to-one

Now let's look at this relation from the perspective of a product. A product belongs to a shop, so you can access the shop like this:

    $shop $product->shop;

This is called the 'parent bean'.
The shop is considered the parent of the product. It owns the product.

Either (5.7.3+)

The PHP ?? operator does not work properly because of lazy loading, you can use either() instead:

    $text
        
->either()
        ->
page
        
->book
        
->title
        
->_or('nothing');

The Either object also works with lists and offers a convenient first() and last() method:

    $book
        
->either()
        ->
ownPageList
        
->first()
        ->
ownParagraphList
        
->last()
        ->
id
        
->_or(0);

Exists (5+)

To check if a related bean exists:

    $product->exists('shop');

This function will return TRUE if a shop has been associated with the product and FALSE otherwise.

Setting a parent bean

To set a parent bean:

    $product->shop $someShop;
    
R::store$product );

Note that, when you set a new shop the property shop_id still points to the old shop (or is still NULL if there was no previous shop). This field gets updated after the bean has been saved. So, the shop_id and shop fields are not always in sync.

    $product->shop $newShop;
    echo 
$product->shop_id//still old value
    
R::store$product );
    echo 
$product->shop_id//has been updated!

Another way to update the shop is to simply set the new id, once again, shop_id and shop will be out-of-sync until the next R::store().

    $oldShop $product->shop;
    
$product->shop_id $newID;
    echo 
$product->shop->id//old id
    
R::store$product );
    echo 
$product->shop->id//new id

However if we change the id before we load the shop, the new shop will be loaded:

    $product->shop_id $newID;
    echo 
$product->shop->id//old id
    
R::store$product );
    echo 
$product->shop->id//new id

This may seem a bit weird but it's actually quite logical. When accessing the parent bean, RedBeanPHP simply looks at the value of shop_id and loads the shop identified by that id.

As of RedBeanPHP 4.3.4 the latter behavior has been resolved. Changing the _id property after loading will also sync the loaded bean.

Removing the parent

To remove the shop from our product in the example above, simply assign the value NULL to the property 'shop':

    $product->shop NULL//removes product from shop

Besides one-to-many, RedBeanPHP has a special version of this relation: the one-to-X also known as the one-to-fixed relation. Read more about the One-to-fixed relation.


back to main menu

Donate to RedBeanPHP using Monero:
47mmY3AVbRu 7zVVd4bxQnzD
2jR7PQtBJ cF93jWHQ
rP7yRED4qr fqu6G9Q8ZNu7
zqwnB28rz76 w7MaExf
mALVg69yFd 9sUmz
(remove spaces and new lines)

Performance monitor: this page has been generated in 0.016520977020264s. Is the performance lacking? Please drop me an e-mail to notify me!

Partners  
Uurboek.nl PapelDigital

 

RedBeanPHP Easy ORM for PHP © 2024 () and the RedBeanPHP community () - Licensed New BSD/GPLv2 - RedBeanPHP Archives
RedBeanPHP, the power ORM for PHP since 2009.

Privacy Statement