31 public static function remove($instance): ?int {
33 $className = \get_class($instance);
46 private static function removeByKey_($className, $tableName, $keyAndValues): ?int {
47 $db = self::getDb($className);
48 $sql =
'DELETE FROM ' . $db->quote . $tableName . $db->quote .
' WHERE ' . SqlUtils::getWhere($keyAndValues);
49 Logger::info(
'DAOUpdates', $sql,
'delete');
50 $statement = $db->prepareStatement($sql);
52 if ($statement->execute($keyAndValues)) {
53 return $statement->rowCount();
55 }
catch (\PDOException $e) {
56 Logger::warn(
'DAOUpdates', $e->getMessage(),
'delete');
71 private static function remove_($db, $tableName, $where, $params) {
72 $sql =
'DELETE FROM ' . $tableName .
' ' . SqlUtils::checkWhere($where);
73 Logger::info(
'DAOUpdates', $sql,
'delete');
74 $statement = $db->prepareStatement($sql);
76 if ($statement->execute($params)) {
77 return $statement->rowCount();
79 }
catch (\PDOException $e) {
80 Logger::warn(
'DAOUpdates', $e->getMessage(),
'delete');
93 public static function deleteAll($modelName, $where, $params = []) {
94 $db = self::getDb($modelName);
96 $tableName = OrmUtils::getTableName($modelName);
97 return self::remove_($db, $quote . $tableName . $quote, $where, $params);
101 $modelName = \get_class($instance);
102 $db = self::getDb($modelName);
105 $pkv = OrmUtils::getFirstKeyValue($instance);
107 $tableName = $manyParser->getJoinTable();
108 $fkManyField = $manyParser->getMyFkField();
109 return self::remove_($db, $quote . $tableName . $quote,
"$fkManyField= ?", [$pkv]);
113 $getter =
'get' . \ucfirst($memberName);
114 $instances = $instance->{$getter}();
115 foreach ($instances as $o) {
122 $classname = \get_class($instance);
123 $oneToManyMembers = OrmUtils::getRemoveCascadeFields($classname);
125 $count = \count($oneToManyMembers);
126 while ($i < $count) {
127 self::deleteOneToManyChildren($instance, $oneToManyMembers[$i]);
131 $manyToManyMembers = OrmUtils::getRemoveCascadeFields($classname,
'#manyToMany');
133 $count = \count($manyToManyMembers);
134 while ($j < $count) {
135 self::deleteManyToManyChildren($instance, $manyToManyMembers[$j]);
149 public static function updateAll(
string $modelName, array $values,
string $condition, array $params) {
150 $db = self::getDb($modelName);
152 $tableName = OrmUtils::getTableName($modelName);
154 $allParams = \array_merge($values, $params);
156 $sql =
"UPDATE {$quote}{$tableName}{$quote} SET " . SqlUtils::getUpdateFieldsKeyAndParams($values) . SqlUtils::checkWhereParams($condition, $params);
157 if (Logger::isActive()) {
158 Logger::info(
"DAOUpdates", $sql,
"updateAll");
159 Logger::info(
"DAOUpdates", \json_encode($allParams),
"All params");
161 $statement = $db->getUpdateStatement($sql);
163 return $statement->execute($allParams);
164 }
catch (\Exception $e) {
165 Logger::warn(
"DAOUpdates", $e->getMessage(),
"updateAll");
177 public static function delete($modelName, $ids) {
178 $tableName = OrmUtils::getTableName($modelName);
179 $db = self::getDb($modelName);
180 $pk = OrmUtils::getFirstKey($modelName);
181 if (!\is_array($ids)) {
185 $count = \count($ids);
186 $r = $quote . $pk . $quote .
"= ?";
187 return self::remove_($db, $quote . $tableName . $quote, \str_repeat(
"$r OR", $count - 1) . $r, $ids);
191 $tableName = OrmUtils::getTableName($modelName);
192 if (!\is_array($id)) {
193 $id = [OrmUtils::getFirstKey($modelName) => $id];
195 return self::removeByKey_($modelName, $tableName, $id);
204 public static function insert($instance, $insertMany =
false) {
205 EventsManager::trigger(
'dao.before.insert', $instance);
206 $className = \get_class($instance);
207 $db = self::getDb($className);
209 $tableName = OrmUtils::getTableName($className);
210 $keyAndValues = Reflexion::getPropertiesAndValues($instance);
211 $keyAndValues = \array_merge($keyAndValues, OrmUtils::getManyToOneMembersAndValues($instance));
212 $pk = OrmUtils::getFirstKey($className);
213 $pkVal = $keyAndValues [$pk] ??
null;
214 if (($pkVal) ==
null) {
215 unset ($keyAndValues [$pk]);
217 $sql =
"INSERT INTO {$quote}{$tableName}{$quote} (" . SqlUtils::getInsertFields($keyAndValues) .
') VALUES(' . SqlUtils::getInsertFieldsValues($keyAndValues) .
')';
218 if (Logger::isActive()) {
219 Logger::info(
'DAOUpdates', $sql,
'insert');
220 Logger::info(
'DAOUpdates', \json_encode($keyAndValues),
'Key and values');
223 $statement = $db->getUpdateStatement($sql);
225 $result = $statement->execute($keyAndValues);
227 if ($pkVal ==
null) {
228 $lastId = $db->lastInserId(
"{$tableName}_{$pk}_seq");
230 $propKey = OrmUtils::getFirstPropKey($className);
231 $propKey->setValue($instance, $lastId);
235 $instance->_rest = $keyAndValues;
236 $instance->_rest [$pk] = $pkVal;
239 self::insertOrUpdateAllManyToMany($instance);
242 EventsManager::trigger(DAOEvents::AFTER_INSERT, $instance, $result);
244 }
catch (\Exception $e) {
245 Logger::warn(
'DAOUpdates', $e->getMessage(),
'insert');
246 if (Startup::$config [
'debug']) {
259 $members = OrmUtils::getAnnotationInfo(get_class($instance),
'#manyToMany');
260 if ($members !==
false) {
261 $members = \array_keys($members);
262 foreach ($members as $member) {
263 self::insertOrUpdateManyToMany($instance, $member);
275 $db = self::getDb(\get_class($instance));
277 if ($parser->init()) {
279 $myField = $parser->getMyFkField();
280 $field = $parser->getFkField();
281 $sql =
"INSERT INTO {$quote}" . $parser->getJoinTable() .
"{$quote}({$quote}" . $myField .
"{$quote},{$quote}" . $field .
"{$quote}) VALUES (:" . $myField .
",:" . $field .
");";
282 $memberAccessor =
'get' . \ucfirst($member);
283 $memberValues = $instance->$memberAccessor ();
284 $myKey = $parser->getMyPk();
285 $myAccessorId =
'get' . \ucfirst($myKey);
286 $accessorId =
'get' . \ucfirst($parser->getPk());
287 $id = $instance->$myAccessorId ();
288 if (!\is_null($memberValues)) {
289 $db->execute(
"DELETE FROM {$quote}" . $parser->getJoinTable() .
"{$quote} WHERE {$quote}{$myField}{$quote}='{$id}'");
290 $statement = $db->prepareStatement($sql);
291 foreach ($memberValues as $targetInstance) {
292 $foreignId = $targetInstance->$accessorId ();
293 $foreignInstances = self::getAll($parser->getTargetEntity(), $quote . $parser->getPk() . $quote .
"='{$foreignId}'");
294 if (!OrmUtils::exists($targetInstance, $parser->getPk(), $foreignInstances)) {
295 self::insert($targetInstance,
false);
296 $foreignId = $targetInstance->$accessorId ();
297 Logger::info(
'DAOUpdates',
"Insertion d'une instance de " . get_class($instance),
'InsertMany');
299 $db->bindValueFromStatement($statement, $myField, $id);
300 $db->bindValueFromStatement($statement, $field, $foreignId);
301 $statement->execute();
302 Logger::info(
'DAOUpdates',
"Insertion des valeurs dans la table association '" . $parser->getJoinTable() .
"'",
'InsertMany');
315 public static function update($instance, $updateMany =
false) {
316 EventsManager::trigger(
'dao.before.update', $instance);
317 $className = \get_class($instance);
318 $db = self::getDb($className);
320 $tableName = OrmUtils::getTableName($className);
321 $ColumnskeyAndValues = \array_merge(Reflexion::getPropertiesAndValues($instance), OrmUtils::getManyToOneMembersAndValues($instance));
322 $keyFieldsAndValues = $instance->_pkv ?? OrmUtils::getKeyFieldsAndValues($instance,
'___');
323 $allParameterValues = $ColumnskeyAndValues + $keyFieldsAndValues;
324 $sql =
"UPDATE {$quote}{$tableName}{$quote} SET " . SqlUtils::getUpdateFieldsKeyAndParams($ColumnskeyAndValues) .
' WHERE ' . SqlUtils::getWherePK($keyFieldsAndValues);
325 if (Logger::isActive()) {
326 Logger::info(
"DAOUpdates", $sql,
"update");
327 Logger::info(
"DAOUpdates", \json_encode($allParameterValues),
"Key and values");
329 $statement = $db->getUpdateStatement($sql);
331 $result = $statement->execute($allParameterValues);
332 if ($updateMany && $result) {
333 self::insertOrUpdateAllManyToMany($instance);
335 EventsManager::trigger(DAOEvents::AFTER_UPDATE, $instance, $result);
336 $instance->_rest = \array_merge($instance->_rest, $ColumnskeyAndValues);
338 }
catch (\Exception $e) {
339 Logger::warn(
"DAOUpdates", $e->getMessage(),
"update");
352 public static function updateGroup($instances, $updateMany =
false) {
353 if (\count($instances) > 0) {
354 $instance = \current($instances);
355 $className = \get_class($instance);
356 $db = self::getDb($className);
358 $tableName = OrmUtils::getTableName($className);
359 $ColumnskeyAndValues = \array_merge(Reflexion::getPropertiesAndValues($instance), OrmUtils::getManyToOneMembersAndValues($instance));
360 $keyFieldsAndValues = OrmUtils::getKeyFieldsAndValues($instance);
361 $sql =
"UPDATE {$quote}{$tableName}{$quote} SET " . SqlUtils::getUpdateFieldsKeyAndParams($ColumnskeyAndValues) .
' WHERE ' . SqlUtils::getWhere($keyFieldsAndValues);
363 $statement = $db->getUpdateStatement($sql);
365 $db->beginTransaction();
366 foreach ($instances as $instance) {
367 EventsManager::trigger(
'dao.before.update', $instance);
368 $ColumnskeyAndValues = \array_merge(Reflexion::getPropertiesAndValues($instance), OrmUtils::getManyToOneMembersAndValues($instance));
369 $result = $statement->execute($ColumnskeyAndValues);
370 if ($updateMany && $result) {
371 self::insertOrUpdateAllManyToMany($instance);
373 EventsManager::trigger(DAOEvents::AFTER_UPDATE, $instance, $result);
374 $instance->_rest = \array_merge($instance->_rest, $ColumnskeyAndValues);
375 if (Logger::isActive()) {
376 Logger::info(
"DAOUpdates", $sql,
"update");
377 Logger::info(
"DAOUpdates", json_encode($ColumnskeyAndValues),
"Key and values");
382 }
catch (\Exception $e) {
383 Logger::warn(
"DAOUpdates", $e->getMessage(),
"update");
396 public static function save($instance, $updateMany =
false) {
397 if (isset ($instance->_rest)) {
398 return self::update($instance, $updateMany);
400 return self::insert($instance, $updateMany);
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Object/relational mapping utilities.
static getTableName($class)
static getKeyFieldsAndValues($instance, $prefix='')
Reflection utilities in dev environment only.
static updateGroup($instances, $updateMany=false)
Updates an array of $instances in the database.
static deleteById($modelName, $id)
static deleteAllChildren(object $instance)
static update($instance, $updateMany=false)
Updates an existing $instance in the database.
static updateAll(string $modelName, array $values, string $condition, array $params)
Mass update for $modelName matching the condition $where, with an associative array of $values.
static insertOrUpdateManyToMany($instance, $member)
Updates the $member member of $instance annotated by a ManyToMany.
static remove_($db, $tableName, $where, $params)
static deleteManyToManyChildren(object $instance, string $memberName)
static insertOrUpdateAllManyToMany($instance)
Updates manyToMany members.
static insert($instance, $insertMany=false)
Inserts a new instance $instance into the database.
static deleteAll($modelName, $where, $params=[])
Deletes all instances from $modelName matching the condition $where.
static deleteOneToManyChildren(object $instance, string $memberName)
static removeByKey_($className, $tableName, $keyAndValues)
static save($instance, $updateMany=false)
Ubiquity\orm\traits This class is part of Ubiquity.