Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
CRUDHelper.php
Go to the documentation of this file.
1<?php
2
4
12
22
23 public static function getIdentifierFunction($model) {
24 $pks = OrmUtils::getKeyMembers ( $model );
25 return function ($index, $instance) use ($pks) {
26 $values = [ ];
27 foreach ( $pks as $pk ) {
28 $getter = 'get' . \ucfirst ( $pk );
29 if (\method_exists ( $instance, $getter )) {
30 $values [] = $instance->{$getter} ();
31 }
32 }
33 return \implode ( '_', $values );
34 };
35 }
36
37 public static function search($model, $search, $fields, $initialCondition = '1=1') {
38 $words = \preg_split ( "@(\s*?(\‍(|\‍)|\|\||\&\&)\s*?)@", $search );
39 $params = [ ];
40 $count = \count ( $fields );
41 $db = DAO::getDb ( $model );
42 $like = $db->getSpecificSQL ( 'tostring' ) . ' LIKE ';
43 SqlUtils::$quote = $db->quote;
44 if ($words !== false) {
45 $words = array_filter ( $words, 'strlen' );
46 $condition = "(" . SqlUtils::getSearchWhere ( $like, $fields, '?', '', '' ) . ")";
47 foreach ( $words as $word ) {
48 $word = \trim ( $word );
49 $params = [ ...$params,...(\array_fill ( 0, $count, "%{$word}%" )) ];
50 }
51
52 $condition = \str_replace ( '||', ' OR ', $condition );
53 $condition = \str_replace ( '&&', ' AND ', $condition );
54 $condition = '(' . $condition . ') AND ' . $initialCondition . '';
55 } else {
56 $condition = $initialCondition;
57 }
58 return DAO::getAll ( $model, $condition, false, $params );
59 }
60
61 public static function update($instance, $values, $setValues = true, $updateMany = true, $eventCallback = null) {
62 $className = \get_class ( $instance );
63 $fieldsInRelationForUpdate = OrmUtils::getFieldsInRelationsForUpdate_ ( $className );
64 $manyToOneRelations = $fieldsInRelationForUpdate ['manyToOne'];
65 $manyToManyRelations = $fieldsInRelationForUpdate ['manyToMany'];
66 $oneToManyRelations = $fieldsInRelationForUpdate ['oneToMany'];
67
68 $members = \array_keys ( $values );
69 OrmUtils::setFieldToMemberNames ( $members, $fieldsInRelationForUpdate ['relations'] );
70 $update = false;
71
72 self::setInputValues($className, $instance, $values, $setValues);
73
74 if ($manyToOneRelations) {
75 self::updateManyToOne ( $manyToOneRelations, $members, $className, $instance, $values );
76 }
77 if (isset ( $instance )) {
78 if (isset ( $eventCallback )) {
79 $eventCallback ( $instance, $instance->_new );
80 }
81 if ($instance->_new) {
82 $update = DAO::insert ( $instance );
83 } else {
84 $update = DAO::update ( $instance );
85 if (DbCache::$active) {
86 // TODO update dbCache
87 }
88 }
89 if($updateMany && $update){
90 if ($manyToManyRelations) {
91 self::updateManyToMany ( $manyToManyRelations, $members, $className, $instance, $values );
92 }
93 if ($oneToManyRelations) {
94 self::updateOneToMany ( $oneToManyRelations, $members, $className, $instance, $values );
95 }
96 }
97 }
98 return $update;
99 }
100
101 protected static function setInputValues(string $className,$instance,&$values,$setValues){
102 $fieldTypes = OrmUtils::getFieldTypes ( $className );
103 foreach ( $fieldTypes as $property => $type ) {
104 if (DbTypes::isBoolean($type)) {
105 if (isset ( $values [$property] )) {
106 $values [$property] = 1;
107 } else {
108 $values [$property] = 0;
109 }
110 }
111 }
112 if ($setValues) {
113 URequest::setValuesToObject ( $instance, $values );
114 }
115 }
116
117 private static function getInputValues($values,$index){
118 $r=[];
119 foreach ($values as $k=>$oValues){
120 if($k!=='_status') {
121 $r[$k] = $oValues[$index];
122 }
123 }
124 return $r;
125 }
126
127 private static function getOneToManyKeys($keys,$values,$index,$defaultId){
128 $r=[];
129 foreach ( $keys as $k){
130 $nk=$values[$k][$index]??$defaultId;
131 $r[$k]=$nk;
132 if($nk==''){
133 return false;
134 }
135 }
136 return $r;
137 }
138
139 protected static function updateOneToMany($oneToManyRelations,$members,$className,$instance,$values){
140 $id=OrmUtils::getFirstKeyValue($instance);
141 $newValues=[];
142 foreach ($oneToManyRelations as $name){
143 $member=$name.'Ids';
144 $len=\strlen($member);
145 if(($values[$member]??'')==='updated'){
146 foreach ($values as $k=>$v){
147 if(\substr($k, 0, $len) === $member){
148 $newK=\substr($k,$len+1);
149 if($newK!=null) {
150 $newValues[$newK] = $v;
151 }
152 }
153 }
154 $r=OrmUtils::getAnnotationInfoMember($className,'#oneToMany',$name);
155 $fkClass=$r['className'];
156 $keys=\array_keys(OrmUtils::getKeyFields($fkClass));
157 foreach ($newValues['_status'] as $index=>$status){
158 $kv=self::getOneToManyKeys($keys,$newValues,$index,$id);
159 if($kv!==false) {
160 switch ($status) {
161 case 'deleted':
162 DAO::deleteById($fkClass,$kv);
163 break;
164 case 'updated':
165 $o = DAO::getById($fkClass, $kv);
166 if ($o) {
167 $oValues = self::getInputValues($newValues, $index);
168 self::setInputValues($fkClass, $o, $oValues, true);
169 DAO::update($o);
170 }
171 break;
172 case 'added':
173 $o=new $fkClass();
174 $oValues = \array_merge($kv,self::getInputValues($newValues, $index));
175 self::setInputValues($fkClass, $o, $oValues, true);
176 DAO::insert($o);
177 break;
178 }
179 }
180 }
181 }
182 }
183 }
184
185 protected static function updateManyToOne($manyToOneRelations, $members, $className, $instance, $values) {
186 foreach ( $manyToOneRelations as $member ) {
187 if (\array_search ( $member, $members ) !== false) {
188 $joinColumn = OrmUtils::getAnnotationInfoMember ( $className, '#joinColumn', $member );
189 if ($joinColumn) {
190 $fkClass = $joinColumn ['className'];
191 $fkField = $joinColumn ['name'];
192 if (isset ( $values [$fkField] )) {
193 if ($values [$fkField] != null) {
194 $fkObject = DAO::getById ( $fkClass, $values ["$fkField"] );
195 Reflexion::setMemberValue ( $instance, $member, $fkObject );
196 } elseif ($joinColumn ['nullable'] ?? false) {
197 Reflexion::setMemberValue ( $instance, $member, null );
198 }
199 }
200 }
201 }
202 }
203 }
204
205 protected static function updateManyToMany($manyToManyRelations, $members, $className, $instance, $values) {
206 foreach ( $manyToManyRelations as $member ) {
207 if (\array_search ( $member, $members ) !== false) {
208 if (($annot = OrmUtils::getAnnotationInfoMember ( $className, '#manyToMany', $member )) !== false) {
209 $newField = $member . 'Ids';
210 $fkClass = $annot ['targetEntity'];
211 $fkObjects = DAO::getAll ( $fkClass, self::getMultiWhere ( $values [$newField], $fkClass ) );
212 if (Reflexion::setMemberValue ( $instance, $member, $fkObjects )) {
213 DAO::insertOrUpdateManyToMany ( $instance, $member );
214 }
215 }
216 }
217 }
218 }
219
220 private static function getMultiWhere($ids, $class) {
221 $pk = OrmUtils::getFirstKey ( $class );
222 $ids = explode ( ',', $ids );
223 $idCount = \count ( $ids );
224 if ($idCount < 1)
225 return '';
226 $strs = [ ];
227 for($i = 0; $i < $idCount; $i ++) {
228 $strs [] = $pk . "='" . $ids [$i] . "'";
229 }
230 return \implode ( " OR ", $strs );
231 }
232
233 public static function getFkIntance($instance, $model, $member, $included = false) {
234 $result = [ ];
235 if (($annot = OrmUtils::getAnnotationInfoMember ( $model, '#oneToMany', $member )) !== false) {
236 $objectFK = DAO::getOneToMany ( $instance, $member, $included );
237 $fkClass = $annot ['className'];
238 } elseif (($annot = OrmUtils::getAnnotationInfoMember ( $model, '#manyToMany', $member )) !== false) {
239 $objectFK = DAO::getManyToMany ( $instance, $member );
240 $fkClass = $annot ['targetEntity'];
241 } else {
242 $objectFK = Reflexion::getMemberValue ( $instance, $member );
243 if ($objectFK!=null && ! is_object ( $objectFK )) {
244 $objectFK = DAO::getManyToOne ( $instance, $member, $included );
245 }
246 if (isset ( $objectFK ))
247 $fkClass = \get_class ( $objectFK );
248 }
249 if (isset ( $fkClass )) {
250 $fkTable = OrmUtils::getTableName ( $fkClass );
251 $result [$member] = compact ( 'objectFK', 'fkClass', 'fkTable' );
252 }
253 return $result;
254 }
255
256 public static function getFKIntances($instance, $model, $included = false) {
257 $result = [ ];
258 $relations = OrmUtils::getFieldsInRelations ( $model );
259 foreach ( $relations as $member ) {
260 $fkInstance = self::getFkIntance ( $instance, $model, $member, $included );
261 if (\count ( $fkInstance ) > 0) {
262 $result = \array_merge ( $result, $fkInstance );
263 }
264 }
265 return $result;
266 }
267}
268
Abstract class for database caching Ubiquity\cache\database$DbCache This class is part of Ubiquity.
Definition DbCache.php:20
Ubiquity\controllers\crud$CRUDHelper This class is part of Ubiquity.
static updateOneToMany($oneToManyRelations, $members, $className, $instance, $values)
static search($model, $search, $fields, $initialCondition='1=1')
static getFKIntances($instance, $model, $included=false)
static updateManyToMany($manyToManyRelations, $members, $className, $instance, $values)
static getInputValues($values, $index)
static getFkIntance($instance, $model, $member, $included=false)
static update($instance, $values, $setValues=true, $updateMany=true, $eventCallback=null)
static getOneToManyKeys($keys, $values, $index, $defaultId)
static updateManyToOne($manyToOneRelations, $members, $className, $instance, $values)
static setInputValues(string $className, $instance, &$values, $setValues)
SQL utilities.
Definition SqlUtils.php:13
Manage Databases types.
Definition DbTypes.php:14
Gateway class between database and object model.
Definition DAO.php:33
Object/relational mapping utilities.
Definition OrmUtils.php:17
Reflection utilities in dev environment only.
Definition Reflexion.php:17
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18