11<?php
22namespace UserFrosting \Sprinkle \ExtendUser \Database \Models ;
33
4- use UserFrosting \Sprinkle \Core \Database \Models \Model ;
4+ use UserFrosting \Sprinkle \Account \Database \Models \User ;
5+ use UserFrosting \Sprinkle \ExtendUser \Database \Models \MemberAux ;
6+ use UserFrosting \Sprinkle \ExtendUser \Database \Scopes \MemberAuxScope ;
57
6- class Member extends Model
8+ trait LinkMemberAux
79{
8- public $ timestamps = true ;
9-
1010 /**
11- * @var string The name of the table for the current model.
11+ * The "booting" method of the trait.
12+ *
13+ * @return void
1214 */
13- protected $ table = "members " ;
15+ protected static function bootLinkMemberAux ()
16+ {
17+ /**
18+ * Create a new MemberAux if necessary, and save the associated member data every time.
19+ */
20+ static ::saved (function ($ member ) {
21+ $ member ->createAuxIfNotExists ();
22+
23+ if ($ member ->auxType ) {
24+ // Set the aux PK, if it hasn't been set yet
25+ if (!$ member ->aux ->id ) {
26+ $ member ->aux ->id = $ member ->id ;
27+ }
28+
29+ $ member ->aux ->save ();
30+ }
31+ });
32+ }
33+ }
34+
35+ class Member extends User
36+ {
37+ use LinkMemberAux;
1438
1539 protected $ fillable = [
16- "user_id " ,
17- "city " ,
18- "country "
40+ 'user_name ' ,
41+ 'first_name ' ,
42+ 'last_name ' ,
43+ 'email ' ,
44+ 'locale ' ,
45+ 'theme ' ,
46+ 'group_id ' ,
47+ 'flag_verified ' ,
48+ 'flag_enabled ' ,
49+ 'last_activity_id ' ,
50+ 'password ' ,
51+ 'deleted_at ' ,
52+ 'city ' ,
53+ 'country '
1954 ];
2055
56+ protected $ auxType = 'UserFrosting\Sprinkle\ExtendUser\Database\Models\MemberAux ' ;
57+
58+ /**
59+ * Required to be able to access the `aux` relationship in Twig without needing to do eager loading.
60+ * @see http://stackoverflow.com/questions/29514081/cannot-access-eloquent-attributes-on-twig/35908957#35908957
61+ */
62+ public function __isset ($ name )
63+ {
64+ if (in_array ($ name , [
65+ 'aux '
66+ ])) {
67+ return true ;
68+ } else {
69+ return parent ::__isset ($ name );
70+ }
71+ }
72+
73+ /**
74+ * Globally joins the `members` table to access additional properties.
75+ */
76+ protected static function boot ()
77+ {
78+ parent ::boot ();
79+
80+ static ::addGlobalScope (new MemberAuxScope );
81+ }
82+
2183 /**
22- * Directly joins the related user, so we can do things like sort, search, paginate, etc.
84+ * Custom mutator for Member property
2385 */
24- public function scopeJoinUser ( $ query )
86+ public function setCityAttribute ( $ value )
2587 {
26- /** @var UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
27- $ classMapper = static :: $ ci -> classMapper ;
28- $ membersTable = $ classMapper -> createInstance ( ' member ' )-> getTable () ;
29- $ usersTable = $ classMapper -> createInstance ( ' user ' )-> getTable ();
88+ $ this -> createAuxIfNotExists ();
89+
90+ $ this -> aux -> city = $ value ;
91+ }
3092
31- $ query = $ query ->select ("$ membersTable.* " );
93+ /**
94+ * Custom mutator for Member property
95+ */
96+ public function setCountryAttribute ($ value )
97+ {
98+ $ this ->createAuxIfNotExists ();
3299
33- $ query = $ query ->leftJoin ($ usersTable , "$ membersTable.user_id " , '= ' , "$ usersTable.id " );
100+ $ this ->aux ->country = $ value ;
101+ }
34102
35- return $ query ;
103+ /**
104+ * Relationship for interacting with aux model (`members` table).
105+ */
106+ public function aux ()
107+ {
108+ return $ this ->hasOne ($ this ->auxType , 'id ' );
36109 }
37110
38111 /**
39- * Get the user associated with this member.
112+ * If this instance doesn't already have a related aux model (either in the db on in the current object), then create one
40113 */
41- public function user ()
114+ protected function createAuxIfNotExists ()
42115 {
43- /** @var UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
44- $ classMapper = static ::$ ci ->classMapper ;
116+ if ($ this ->auxType && !count ($ this ->aux )) {
117+ // Create aux model and set primary key to be the same as the main user's
118+ $ aux = new $ this ->auxType ;
45119
46- return $ this ->belongsTo ($ classMapper ->getClassMapping ('user ' ), 'user_id ' );
120+ // Needed to immediately hydrate the relation. It will actually get saved in the bootLinkMemberAux method.
121+ $ this ->setRelation ('aux ' , $ aux );
122+ }
47123 }
48- }
124+ }
0 commit comments