Application dies on save()

I'm attempting to duplicate the Vokuro security application. With the difference that my database is in PostgreSQL instead of MySQL.

When attempting to sign-up I'm creating a user. But when I attempt to save the user, the application dies and will not progress further. The super odd thing is that the user actually is saved in the database. I can query the DB manually and find the newly created user.

Here is my code I'm using to attempt to debug this problem. (notice all of my ugly echo statements)

    public function signupAction()
    {
        echo 'signupaction';
        $form = new SignUpForm();

        if ($this->request->isPost()) {
            echo 'ispost';
            if ($form->isValid($this->request->getPost()) != false) {
                echo 'isvalid';
                $user = new Users();
                echo 'madeUser';

                $user->assign(array(
                    'name' => $this->request->getPost('name', 'striptags'),
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'profile_id' => 2
                ));
                echo 'assignedUser';
                $savedBool = $user->save();
                echo '<br> User saved = ';
                echo $savedBool;

                if ($savedBool) {
                    echo 'user saved';
                    return $this->dispatcher->forward(array(
                        'controller' => 'index',
                        'action' => 'index'
                    ));
                } else {
                    echo 'user save failed';
                    $messages = '';
                    foreach ($user->getMessages() as $message) {
                        $messages .= $message->getMessage();
                    }

                    $this->flash->error('An error occured, unable to register. Reason: ' . $messages);
                    echo $messages;
                }

                $this->flash->error($user->getMessages());
            }
        }

        $this->view->form = $form;
    }

Now this is EXACTLY what my application is printing out.

signupactionispostisvalidmadeUserassignedUser

So I hit my echo statement for "assignedUser" Then I run the save() function on $user. And my application dies. It doesn't reach the next echo statement.

I'm at a loss and I'm not sure what's wrong here. Since the user actually was sent to the database. I set up a query logger and the insert statement is being run correctly. Why would the application die here?



18.5k

Does your apache/mysql error logs tell you something?

edited Sep '14

Unfortunately no. I don't see anything in my apache or mysql logs. Any page is loading just fine and producing html from the volt templates, except when there is a database save() command.

These are the queries being logged

[Sun, 21 Sep 14 22:55:41 +0200][INFO] SELECT COUNT(*) AS "rowcount" FROM "users" WHERE "users"."email" = :0

[Sun, 21 Sep 14 23:21:57 +0200][INFO] SELECT COUNT(*) AS "rowcount" FROM "users" WHERE "users"."email" = :0

[Sun, 21 Sep 14 23:21:58 +0200][INFO] INSERT INTO "users" ("name", "email", "password", "mustchangepassword", "profile_id", "banned", "suspended", "active", "id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, default)

[Sun, 21 Sep 14 23:21:58 +0200][INFO] INSERT INTO "emailconfirmations" ("userid", "code", "createdon", "updatedon", "confirmed", "id") VALUES (?, ?, ?, null, ?, default)

[Sun, 21 Sep 14 23:21:58 +0200][INFO] SELECT "users"."id", "users"."name", "users"."email", "users"."password", "users"."mustchangepassword", "users"."profile_id", "users"."banned", "users"."suspended", "users"."active" FROM "users" WHERE "users"."id" = :0 LIMIT :2

And the queries are successful. I have a new user row and a new email confirmation row inside my database.

This function is supposed to be returning a boolean, but instead it's just dying.



13.1k
edited Oct '14

Hi,

I ran into the same problem using a different database ( SQL Server ). The problem is with the Model->save() function. It checks if the record was inserted successfully by retrieving the last inserted record's id. To overcome this I use the following functions instead for CRUD operations.

YourModel->create(); // for new records
YourModel->update(); //for updates

I hope this helps - Reference : http://docs.phalconphp.com/pt/latest/reference/models.html

Create/Update with Confidence

When an application has a lot of competition, we could be expecting create a record but it is actually updated. This could happen if we use Phalcon\Mvc\Model::save() to persist the records in the database. If we want to be absolutely sure that a record is created or updated, we can change the save() call with create() or update():

    <?php

    $robot       = new Robots();
    $robot->type = "mechanical";
    $robot->name = "Astro Boy";
    $robot->year = 1952;

    //This record only must be created
    if ($robot->create() == false) {
    echo "Umh, We can't store robots right now: \n";
    foreach ($robot->getMessages() as $message) {
        echo $message, "\n";
    }
    } else {
    echo "Great, a new robot was created successfully!";
    }

These methods “create” and “update” also accept an array of values as parameter.

Regards