Numbered Posted November 20, 2017 Posted November 20, 2017 Just now i understand my wrong understanding of load method) if i try to load member by his id, which is not exist on forum ($member = \IPS\Member::load(123456789)->member_id - returned null), set some params $member->name = 'somename'; and call $member->save() - it will save with new member_id from auto_increment value. But i tried to load 123456789! Why it saved in other id filed value? I follow the code changes in ActiveRecord and Member classes and nothing changed finded . My older scripts set $member->member_id = 123456789; in older time before calling ->save(); just for reliability and all worked well. So here is my proposal for improvement. If we try to load member with some id and from catching OutOfRange exception with return new empty member object - may be better for return calling member_id in _new->member_id filed? It will be very logic. For make it we can simply change current that load method public static function load($id, $idField = null, $extraWhereClause = null) { try { if ($id === null OR $id === 0 OR $id === '') { $classname = get_called_class(); return new $classname; } else { $member = parent::load($id, $idField, $extraWhereClause); if ($member->restrict_post > 0 and $member->restrict_post <= time()) { $member->restrict_post = 0; $member->save(); } return $member; } } catch (\OutOfRangeException $e) { $classname = get_called_class(); return new $classname; } } to that: public static function load($id, $idField = null, $extraWhereClause = null) { try { if ($id === null OR $id === 0 OR $id === '') { $classname = get_called_class(); return new $classname; } else { $member = parent::load($id, $idField, $extraWhereClause); if ($member->restrict_post > 0 and $member->restrict_post <= time()) { $member->restrict_post = 0; $member->save(); } return $member; } } catch (\OutOfRangeException $e) { $classname = get_called_class(); $newMember = new $classname; if ($idField === null) { $idField = static::$databasePrefix . static::$databaseColumnId; } $newMember->$idField = $id; return $newMember; } } Extremely logic and good! Yep it's not a bussiness idea or something, which can provide happy to a lot of people. It's just little thing, which will make your powerful platform better. Current logic for example: $member = \IPS\Member::load(123456789); // this member_id is not exist $member->name = 'name'; $member->save(); echo $member->member_id; // return every time new auto_increment id, not 123456789 Newer logic: $member = \IPS\Member::load(123456789); // this member_id is not exist $member->name = 'name'; $member->save(); echo $member->member_id; // return 123456789 and next this calling return already exist member with member_id 123456789. Perfect! Thanks for your attension upd.: And all current scripts not broke by this update. Because newer member_id field puts on object _new part. Not directly. So some other scripts, which call $member = \IPS\Member::load(123456789); if (null !== $member->member_id) { // ... } still working right.
Numbered Posted November 20, 2017 Author Posted November 20, 2017 upd2.: in older time my custom scripts worked good just because i like to use 'hummber' logic. When i call load member, which are not exist and cheked it by null value of member_id returned i set up $member->member id = $already_known_member_id (i already know who i want to load) and calling save with that 'hummer' which provide same result. But i was sure about just load and save will save right member_id which i called in load. I was wrong and it's strange in my mind) Sry for a lot of text. It's funny shock effect
Recommended Posts
Archived
This topic is now archived and is closed to further replies.