Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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();
            }
        }
    }

$languagestring->save() is NOT WORKING properly. $languagestring->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?



366

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);
      }
  }


366
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 :-)



366

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 {

    }