One-to-many and "own" prefix in attribute name

33 views
Skip to first unread message

Vin_rbphp

unread,
Sep 16, 2015, 3:25:22 PM9/16/15
to redbeanphp
I am  using ReadBean along with slim-php, and I have a database that has one-to-many relationship.

E.g.

    $shop = R::dispense( 'shop' );
    $shop->name = 'Antiques';
    $vase = R::dispense( 'product' );
    $vase->price = 25;
    $shop->ownProduct[] = $vase;
    R::store( $shop );
             
    $app->response()->header('Content-Type', 'application/json');   
    echo json_encode(R::exportAll($shop));   


The returned JSON is :

[
  {
    "id": "2",
    "name": "Antiques",
    "ownProduct": [
      {
        "id": "2",
        "price": "25",
        "shop_id": "2"
      }
    ]
  }
]

The problem is that instead of "product", it returns "ownProduct" from R::exportAll(). Since the 'own' or 'xown' is only a prefix for one-to-many convention, shouldn't that be removed in the export? Is there a way to get the correct attribute name ("product") during export? 





gabor

unread,
Sep 16, 2015, 5:09:30 PM9/16/15
to redbeanphp

Hi,

Welcome to the RedBeanPHP forum.

To answer your question:

The current approach has the following advantages:

1. No conflicts: you can have 'product' and 'ownProduct' side-by-side
2. Predictable: the key tells you something about the list
3. Consistency: same structure as beans, i.e. property 'ownProduct' translates to 'ownProduct'
4. Readability (subjective): the relations are easy to spot
5. Implementation: there are several powerful features associated with the implementation, however to benefit from those we need the 'same' keys as the beans

cheers
Gabor

Reply all
Reply to author
Forward
0 new messages