gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

[Question] Complex Find's #767

Closed benzon closed 4 years ago

benzon commented 4 years ago

Hi might not be the right place.

But trying to understand RedBeanPHP, one thing i can't figure out tho, if i use the ownLists.

Is there anyway with find to only get ownEntrys with a specific value, ex, let's say i did a layout like this

Username Birthdate ownInfo - > ownInfo.age - > ownInfo.location

And i only want to get beans where the age is let's say 30, is there any way to do this with redbean, or do i have to get a list from the ownInfo table and then use ex the user_id to get the rest of the data?

gabordemooij commented 4 years ago

You can use withCondition to filter lists:

$bean->withCondition(' age = ? ', [30])->ownInfo;

More details: https://redbeanphp.com/index.php?p=/using_sql_snippets

If you need to use criteria from multiple tables, a simple R::find() might be better though.

benzon commented 4 years ago

But how would it work with find ? how would i be able to do the conditions there just wondering?

Just thinking in the perspective of a search enigne function, since find might be the better solution.

benzon commented 4 years ago
    $user = R::dispense('users');
    $user->name = 'BenZoN';
    $user->birthdate = '1983-08-25';

    $status = R::dispense('status');
    $status->whitelist = 0;
    $status->ems = 0;
    $status->police = 0;
    $status->ped = 0;
    $status->gang = 0;

    $arr = array(
        'charName' => 'Søren Benzon Eskildsen',
        'age' => 31,
        'description' => 'Blah Blah',
        'experience' => 'Blah Blah',
        'experience_ref' => 'DPL og DRP',
        'freetext' => '',
        'explain' => '',
        'rules' => '',
        'reaction_one' => '',
        'reaction_two' => '',
        'date' => date("Y-m-d H:i:s"),
    );

    $applicationJson = json_encode($arr);
    $application1 = R::dispense('application');
    $application1->type = 'whitelist';
    $application1->status = 'wait';
    $application1->application = $applicationJson;

    $user->ownStatusList[] = $status;
    $user->ownApplication[] = $application1;

    $id = R::store($user);
benzon commented 4 years ago

This is my layout, what im trying to do is only get ex users where whitelist = 1 from status, not shure how to do this tho, and if its even possible.

Found a way to load all data base entrys before including all the own's now i can remember what that was damit :D

Lynesth commented 4 years ago

Hey there,

Each status is only linked to 1 user and each user only has 1 status? If so that's a one-to-one relation and status should be included in the user table, according to RedBean philosophy.

You can still get all users you are looking for this way:

$whitelisted_users = R::find( 'user', ' JOIN `status` WHERE `user`.id = `status`.user_id AND `status`.whitelist = ? ', [ 1 ] );