We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Double save transaction at once

Hello,

I have a problem with saving records on validation process. Problem description:

1) Trying to save() model with some data

2) $client->save() going to $client model and trying to validate data passed from assigning to model (ex. $client->setEmail($form_data['email']);) for uniqueness.

3) Calling function on $client model

    public function validation()
    {
        $this->validate(new \Phalcon\Mvc\Model\Validator\Uniqueness(array(
            "field" => "email",
            "message" => $this->getDI()->get('translate')->_('Email already exists')
        )));
        return $this->validationHasFailed() != true;
    }

4) When script undertands that e-mail exists then going to translation module and get the translation for message string "Email already exists"

5) On my custom translation adapter there is a code contains $this->createLanguageString($translation); $translation is a string passed from model $client

6) And here is a problem.....

    public function createLanguageString($translation)
    {
        $language_string = new LanguageString();
        $language_string->setStringName($translation);

        if (!$language_string->save()) {
            foreach ($language_string->getMessages() as $message) {
                echo $message->getMessage();
            }
        }
    }

$language_string->save() is NOT WORKING properly. $language_string->save() do not return any error. Debugger shows that record is going into the database and database or script returns an INSERT ID, but...... There is NO RECORD IN DATABASE!

Maybe it is because Phalcon do not give to save any other records when previous save() is not finished? Please help. Thank you



4.0k

Can you paste controller action?



727

Yes..

public function createAction ()
    {
      $this->view->setRenderLevel(View::LEVEL_NO_RENDER);
      if ($this->request->isAjax() && $this->request->isPost()) {

          $client = new Client();
          $form = new ClientForm($client, array('create' => true));
          $resData = array();

          parse_str($this->request->getPost('form_data'), $form_data);

          if (!$form->isValid($form_data)) {
              foreach($form->getMessages() as $message) {
                  $resData['errors'][] = $message->getMessage();
              }
          } else {

              $client->setName($form_data['name']);
              $client->setWww($form_data['www']);
              $client->setClientDomain($form_data['client_domain']);
              $client->setDbName($form_data['db_name']);
              $client->setEmail($form_data['email']);
              if (isset($form_data['password'])) $client->setPassword($this->security->hash($form_data['password']));
              $client->setPhone($form_data['phone']);
              $client->setAdditionalInformation($form_data['additional_information']);

              if (!$client->save()) {
                  foreach ($client->getMessages() as $message) {
                      $resData['errors'][] = $message->getMessage();
                  }
              } else {
                 $resData['success'] = $this->translate->_('Client was successfully created!');
              }
          }
          echo json_encode($resData);
      }
  }


727
Accepted
answer

Now after searching an answer i got the situation:

If validation fails -> $client transaction is rolling back. When I try to INSERT some other data with other transaction in same time (ex. to save LanguageString) then ALL operations are rolling back.

Now I am trying to save needed transaction with Phalcon\Mvc\Model\Transaction\Manager like this

    $manager = new TxManager();
    $transaction = $manager->get();

    $language_string = new LanguageString();
    $language_string->setTransaction($transaction);
    $language_string->setModuleId($this->module_id);
    $language_string->setStringName($translation);

    if ($language_string->save() == false) {
        $transaction->rollback();
    }else{
        $transaction->commit();
    }

Then there is NO luck to do. Debuger shows that $transaction->commit(); making a commit, but there is NO value in database. Please help :-)



727

Made this the other way.

    $this->validate(new \Phalcon\Mvc\Model\Validator\Uniqueness(array(
        "field" => "www",
        "message" => 'Domain name already exists'
    )));

In controller:

    if (!$client->save()) {
      foreach ($client->getMessages() as $message) {
        $resData['errors'][] = $this->translate->_($message->getMessage());
      }
    } else {

    }