File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed
Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 1313
1414namespace think \model \concern ;
1515
16+ use ReflectionClass ;
1617use think \db \exception \ModelEventException ;
1718use think \helper \Str ;
1819
@@ -35,6 +36,13 @@ trait ModelEvent
3536 */
3637 protected $ withEvent = true ;
3738
39+ /**
40+ * 事件观察者.
41+ *
42+ * @var string
43+ */
44+ protected $ eventObserver ;
45+
3846 /**
3947 * 设置Event对象
4048 *
@@ -77,10 +85,17 @@ protected function trigger(string $event): bool
7785 $ call = 'on ' . Str::studly ($ event );
7886
7987 try {
80- if (method_exists (static ::class, $ call )) {
81- $ result = call_user_func ([static ::class, $ call ], $ this );
88+ if ($ this ->eventObserver ) {
89+ $ reflect = new ReflectionClass ($ this ->eventObserver );
90+ $ observer = $ reflect ->newinstance ();
91+ } else {
92+ $ observer = static ::class;
93+ }
94+
95+ if (method_exists ($ observer , $ call )) {
96+ $ result = call_user_func ([$ observer , $ call ], $ this );
8297 } elseif (is_object (self ::$ event ) && method_exists (self ::$ event , 'trigger ' )) {
83- $ result = self ::$ event ->trigger (' model. ' . static ::class . '. ' . $ event , $ this );
98+ $ result = self ::$ event ->trigger (static ::class . '. ' . $ event , $ this );
8499 $ result = empty ($ result ) ? true : end ($ result );
85100 } else {
86101 $ result = true ;
You can’t perform that action at this time.
0 commit comments