Hello,
to update my database, I'd like to compare input value and database value and do a save action only if needed.
The trouble is :
$pageFromDB = Pages::findFirst($id);
$pageFromDB->getTitle() return $data['title'] value and not value from Database
public function updateAction()
{
try{
if (!$this->request->isPost()) {
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $this->dispatcher->forward(
[
"controller" => "pages",
"action" => "index",
]
);
}
$id = $this->request->getPost("id", "int");
$pageFromDB = Pages::findFirst($id);
if (!$pageFromDB) {
$this->flash->error(
"Page does not exist"
);
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $this->dispatcher->forward(
[
"controller" => "pages",
"action" => "index",
]
);
}
$form = new PagesForm();
$data = $this->request->getPost();
// TODO : nettoyer les   et trimer les string
if (!$form->isValid($data, $pageFromDB)) {
$messages = $form->getMessages();
foreach ($messages as $message) {
$this->flash->error($message);
}
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $this->dispatcher->forward(
[
"controller" => "pages",
"action" => "index",
]
);
}
// Create a transaction manager
$manager = new TxManager();
// Request a transaction
$transaction = $manager->get();
// on met à jour le titre de page si modifié
$titleHasChanged = ($pageFromDB->getTitle() !== $data['title'] ? true : false);
if($titleHasChanged)
{
$pageFromDB->setTransaction($transaction);
$pageFromDB->setTitle($data['title']);
if ($pageFromDB->save() === false) {
$messages = $pageFromDB->getMessages();
foreach ($messages as $message) {
$transaction->rollback(
"Page title cannot be updated : " . $message->getMessage()
);
}
}
}
// on recupere les id des elements à suppress et à ajouter
$pagesArticlesToDelete = null;
$articlesIdsToDelete = null;
$articlesIdsToAdd = null;
$this->compareArticlesToUpdate($pageFromDB, $data, $articlesIdsToDelete, $articlesIdsToAdd);
// si on a des PagesArticles à supprimer
// https://forum.phalcon.io/discussion/13751/how-to-delete-relationships-between-models-many-to-many
if(count($articlesIdsToDelete) > 0)
{
// Get the PagesArticles to be deleted
$pagesArticles = PagesArticles::find([
'conditions' => 'idPages = :id: AND idArticles IN ({articlesIdsToDelete:array})',
'bind' => [
'id' => $pageFromDB->getId(),
'articlesIdsToDelete' => $articlesIdsToDelete,
],
]);
foreach ($pagesArticles as $pagesArticle) {
$pagesArticle->setTransaction($transaction);
// Something's gone wrong, we should rollback the transaction
if ($pagesArticle->delete() === false) {
$messages = $pagesArticle->getMessages();
foreach ($messages as $message) {
$transaction->rollback(
"Cannot delete pagesArticles associations : " . $message->getMessage()
);
}
}
}
}
// si on a des PagesArticles à ajouter
if(count($articlesIdsToAdd) > 0)
{
foreach ($articlesIdsToAdd AS $idArticle)
{
$pagesArticles = new PagesArticles();
$pagesArticles->setTransaction($transaction);
$pagesArticles->setIdPages($pageFromDB->getId());
$pagesArticles->setIdArticles($idArticle);
if ($pagesArticles->save() === false) {
$messages = $pagesArticles->getMessages();
foreach ($messages as $message) {
$transaction->rollback(
"Cannot save PagesArticles : " . $message->getMessage()
);
}
}
}
}
if($titleHasChanged || (count($articlesIdsToDelete) > 0) || (count($articlesIdsToAdd) > 0)){
// Everything's gone fine, let's commit the transaction
if ($transaction->commit() == false) {
foreach ($transaction->getMessages() as $message) {
$this->flash->error($message);
}
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $this->dispatcher->forward(
[
"controller" => "pages",
"action" => "new",
]
);
}
}
else
{
$this->flash->warning(
"Page was not updated because it's exactly the same"
);
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $this->dispatcher->forward(
[
"controller" => "pages",
"action" => "index",
]
);
}
$form->clear();
$this->flash->success(
"Page was updated successfully"
);
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $this->dispatcher->forward(
[
"controller" => "pages",
"action" => "index",
]
);
} catch (TxFailed $e) {
// TODO : il faudra mettre un systeme de log pour enregistrer les erreurs
echo "Failed, reason: ", $e->getMessage();
}
}
Thanks for your help