24 $pks = OrmUtils::getKeyMembers ( $model );
25 return function ($index, $instance) use ($pks) {
27 foreach ( $pks as $pk ) {
28 $getter =
'get' . \ucfirst ( $pk );
29 if (\method_exists ( $instance, $getter )) {
30 $values [] = $instance->{$getter} ();
33 return \implode (
'_', $values );
37 public static function search($model, $search, $fields, $initialCondition =
'1=1') {
38 $words = \preg_split (
"@(\s*?(\(|\)|\|\||\&\&)\s*?)@", $search );
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}%" )) ];
52 $condition = \str_replace (
'||',
' OR ', $condition );
53 $condition = \str_replace (
'&&',
' AND ', $condition );
54 $condition =
'(' . $condition .
') AND ' . $initialCondition .
'';
56 $condition = $initialCondition;
58 return DAO::getAll ( $model, $condition,
false, $params );
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'];
68 $members = \array_keys ( $values );
69 OrmUtils::setFieldToMemberNames ( $members, $fieldsInRelationForUpdate [
'relations'] );
72 self::setInputValues($className, $instance, $values, $setValues);
74 if ($manyToOneRelations) {
75 self::updateManyToOne ( $manyToOneRelations, $members, $className, $instance, $values );
77 if (isset ( $instance )) {
78 if (isset ( $eventCallback )) {
79 $eventCallback ( $instance, $instance->_new );
81 if ($instance->_new) {
82 $update = DAO::insert ( $instance );
84 $update = DAO::update ( $instance );
89 if($updateMany && $update){
90 if ($manyToManyRelations) {
91 self::updateManyToMany ( $manyToManyRelations, $members, $className, $instance, $values );
93 if ($oneToManyRelations) {
94 self::updateOneToMany ( $oneToManyRelations, $members, $className, $instance, $values );
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;
108 $values [$property] = 0;
113 URequest::setValuesToObject ( $instance, $values );
119 foreach ($values as $k=>$oValues){
121 $r[$k] = $oValues[$index];
129 foreach ( $keys as $k){
130 $nk=$values[$k][$index]??$defaultId;
139 protected static function updateOneToMany($oneToManyRelations,$members,$className,$instance,$values){
140 $id=OrmUtils::getFirstKeyValue($instance);
142 foreach ($oneToManyRelations as $name){
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);
150 $newValues[$newK] = $v;
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);
162 DAO::deleteById($fkClass,$kv);
165 $o = DAO::getById($fkClass, $kv);
167 $oValues = self::getInputValues($newValues, $index);
168 self::setInputValues($fkClass, $o, $oValues,
true);
174 $oValues = \array_merge($kv,self::getInputValues($newValues, $index));
175 self::setInputValues($fkClass, $o, $oValues,
true);
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 );
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 );
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 );
221 $pk = OrmUtils::getFirstKey ( $class );
222 $ids = explode (
',', $ids );
223 $idCount = \count ( $ids );
227 for($i = 0; $i < $idCount; $i ++) {
228 $strs [] = $pk .
"='" . $ids [$i] .
"'";
230 return \implode (
" OR ", $strs );
233 public static function getFkIntance($instance, $model, $member, $included =
false) {
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'];
242 $objectFK = Reflexion::getMemberValue ( $instance, $member );
243 if ($objectFK!=
null && ! is_object ( $objectFK )) {
244 $objectFK = DAO::getManyToOne ( $instance, $member, $included );
246 if (isset ( $objectFK ))
247 $fkClass = \get_class ( $objectFK );
249 if (isset ( $fkClass )) {
250 $fkTable = OrmUtils::getTableName ( $fkClass );
251 $result [$member] = compact (
'objectFK',
'fkClass',
'fkTable' );
256 public static function getFKIntances($instance, $model, $included =
false) {
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 );
Abstract class for database caching Ubiquity\cache\database$DbCache This class is part of Ubiquity.
Ubiquity\controllers\crud$CRUDHelper This class is part of Ubiquity.
static getMultiWhere($ids, $class)
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 getIdentifierFunction($model)
static updateManyToOne($manyToOneRelations, $members, $className, $instance, $values)
static setInputValues(string $className, $instance, &$values, $setValues)
Gateway class between database and object model.
Object/relational mapping utilities.
Reflection utilities in dev environment only.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.