0

If I have 2 tables one is users and one is stores , the users id field is associated with the store's user_id field . Now if I want to find all those users who has a store how can I perform it on readbean ? and please do explain as I'm just getting started with it. Thanks

Biraj Ghosh
  • 1
  • 1
  • 2

2 Answers2

0

If your queries looking complex, You can simply use plain sql inside redbean.

$records = R::getAll("SELECT * FROM tbl1 LEFT JOIN tbl2 ON tbl1.id = tbl2.tbl1_id");

This will result into and all satisfying records array. Here I have used R::getAll($your_qry) method, to fetch for single row use R::getRow($yoyr_sql_qry); method. If you have any difficulties. let me know.

mahesh kajale
  • 510
  • 1
  • 5
  • 17
0

Is it a 1:Many or Many:Many?

If I understand what you said, it's 1:Many

DB model: stores belong to users

So, a store can belong to exactly one (1) user, correct?

If so, it's easy using redbean

$user = R::dispense('users'); // create a user
$store = R::dispense('stores'); // create a store
$store2 = R::dispense('stores'); // create a store

$store1->name = 'Foo';
$store2->name = 'Bar';

$user->xownStoresList[] = $store; // save user ( and store )
$user->xownStoresList[] = $store2; // save user ( and store )

$id = R::store( $user );

foreach ( $user->ownStoresList as $store ) {
  echo $store->name . ', ';
}

// outputs: "foo, bar,"
Armstrongest
  • 15,181
  • 13
  • 67
  • 106