55
66use Cake \Database \Expression \QueryExpression ;
77use Cake \Database \StatementInterface ;
8- use Cake \I18n \FrozenTime ;
8+ use Cake \Datasource \EntityInterface ;
9+ use Cake \I18n \DateTime ;
910use Cake \ORM \Behavior ;
10- use Cake \ORM \Entity ;
11- use Cake \ORM \Query ;
12- use Cake \ORM \TableRegistry ;
11+ use Cake \ORM \Locator \LocatorAwareTrait ;
12+ use Cake \ORM \Query \SelectQuery ;
1313use LogicException ;
1414
1515/**
1616 * Insert Behavior
1717 */
1818class InsertBehavior extends Behavior
1919{
20+ use LocatorAwareTrait;
21+
2022 /**
2123 * Default config
2224 *
23- * @var array
25+ * @var array<string, mixed>
2426 */
25- protected $ _defaultConfig = [
27+ protected array $ _defaultConfig = [
2628 'event ' => ['beforeSave ' => true ],
2729 ];
2830
2931 /**
3032 * execute bulk insert query
3133 *
32- * @param Entity[] $entities insert entities
33- * @throws LogicException no save data
34- * @return StatementInterface query result
34+ * @param array<\Cake\Datasource\EntityInterface> $entities insert entities
35+ * @throws \ LogicException no save data
36+ * @return \Cake\Database\ StatementInterface query result
3537 */
3638 public function bulkInsert (array $ entities ): StatementInterface
3739 {
@@ -50,7 +52,7 @@ public function bulkInsert(array $entities): StatementInterface
5052 $ fields = array_keys ($ saveData [0 ]);
5153
5254 $ query = $ this ->_table
53- ->query ()
55+ ->insertQuery ()
5456 ->insert ($ fields );
5557 $ query ->clause ('values ' )->setValues ($ saveData );
5658
@@ -60,31 +62,31 @@ public function bulkInsert(array $entities): StatementInterface
6062 /**
6163 * execute insert select query for saving a record just once
6264 *
63- * @param Entity $entity insert entity
65+ * @param \Cake\Datasource\EntityInterface $entity insert entity
6466 * @param array|null $conditions search conditions
65- * @return StatementInterface query result
67+ * @return \Cake\Database\ StatementInterface query result
6668 */
67- public function insertOnce (Entity $ entity , array $ conditions = null ): StatementInterface
69+ public function insertOnce (EntityInterface $ entity , ? array $ conditions = null ): StatementInterface
6870 {
6971 if ($ this ->_config ['event ' ]['beforeSave ' ]) {
7072 $ this ->_table ->dispatchEvent ('Model.beforeSave ' , compact ('entity ' ));
7173 }
7274
7375 $ entity ->setVirtual ([]);
7476 $ insertData = $ entity ->toArray ();
75- if (isset ($ insertData ['created ' ]) && ! is_null ( $ insertData [ ' created ' ]) ) {
76- $ insertData ['created ' ] = FrozenTime ::now ()->toDateTimeString ();
77+ if (isset ($ insertData ['created ' ])) {
78+ $ insertData ['created ' ] = DateTime ::now ()->toDateTimeString ();
7779 }
78- if (isset ($ insertData ['modified ' ]) && ! is_null ( $ insertData [ ' modified ' ]) ) {
79- $ insertData ['modified ' ] = FrozenTime ::now ()->toDateTimeString ();
80+ if (isset ($ insertData ['modified ' ])) {
81+ $ insertData ['modified ' ] = DateTime ::now ()->toDateTimeString ();
8082 }
8183
8284 $ fields = array_keys ($ insertData );
8385 $ existsConditions = $ conditions ;
8486 if (is_null ($ existsConditions )) {
8587 $ existsConditions = $ this ->getExistsConditions ($ insertData );
8688 }
87- $ query = $ this ->_table ->query ()->insert ($ fields );
89+ $ query = $ this ->_table ->insertQuery ()->insert ($ fields );
8890 $ subQuery = $ this
8991 ->buildTmpTableSelectQuery ($ insertData )
9092 ->where (function (QueryExpression $ exp ) use ($ existsConditions ) {
@@ -95,7 +97,7 @@ public function insertOnce(Entity $entity, array $conditions = null): StatementI
9597 return $ exp ->notExists ($ query );
9698 })
9799 ->limit (1 );
98- /* @phpstan-ignore-next-line */
100+
99101 $ query = $ query ->epilog ($ subQuery );
100102
101103 return $ query ->execute ();
@@ -105,10 +107,10 @@ public function insertOnce(Entity $entity, array $conditions = null): StatementI
105107 * build tmp table's select query for insert select query
106108 *
107109 * @param array $insertData insert data
108- * @throws LogicException select query is invalid
109- * @return Query tmp table's select query
110+ * @return \Cake\ORM\Query\SelectQuery tmp table's select query
111+ * @throws \LogicException select query is invalid
110112 */
111- private function buildTmpTableSelectQuery ($ insertData ): Query
113+ private function buildTmpTableSelectQuery (array $ insertData ): SelectQuery
112114 {
113115 $ driver = $ this ->_table
114116 ->getConnection ()
@@ -118,15 +120,15 @@ private function buildTmpTableSelectQuery($insertData): Query
118120 foreach ($ insertData as $ key => $ value ) {
119121 $ col = $ driver ->quoteIdentifier ($ key );
120122 if (is_null ($ value )) {
121- $ schema [] = "NULL AS { $ col} " ;
123+ $ schema [] = "NULL AS $ col " ;
122124 } else {
123125 $ bindKey = ': ' . strtolower ($ key );
124126 $ binds [$ bindKey ] = $ value ;
125- $ schema [] = "{ $ bindKey} AS { $ col} " ;
127+ $ schema [] = "$ bindKey AS $ col " ;
126128 }
127129 }
128130
129- $ tmpTable = TableRegistry:: getTableLocator ()-> get ('tmp ' , [
131+ $ tmpTable = $ this -> fetchTable ('tmp ' , [
130132 'schema ' => $ this ->_table ->getSchema (),
131133 ]);
132134 $ query = $ tmpTable
0 commit comments