简体   繁体   中英

PHP ORM Best Practices: Retrieving values from another object type:

I recently started using RedBean ORM.

I have a User bean type to store the user's info, and an associated FUSE model for user functionality.

When a user logs in, or a session is already set, Redbean retrieves the user's info including address.

Until a user has logged in, I set the state and city values for a fresh user bean via geoip location services.

I am wondering what the best practice is on this type of procedure.

The way I see it, I have a few options:

  1. Create a method in the user FUSE model which instantiates the geoip object and sets the values.
  2. Instantiate the geoip object outside of the FUSE model and set the values directly, ie: $user->state = $location->state;
  3. Instantiate the geoip object outside of the FUSE model, but pass the object to a FUSE model setter which knows how to handle it.

I'm guessing you don't really want to do option one, because it creates a dependency for the class.

Any thoughts?

If you need to process geoip stuff with user every time it loggs in - Create method and initialize it on user login (1). In other option I'd use option 3 ( Instantiate the geoip object outside of the FUSE model, but pass the object to a FUSE model setter which knows how to handle it. ). It's better not to work with object properties directly and set some metods to handle these properties.

$user->setProperty('one'); // Actually can just execute `$user->property = $var;`

but not

$user->property = 'one';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM