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

Model relations

Hello,

I have Users and Sessions I want when Users model save to store the session into Session model, but it fails at that. Users:

public function initialize()
    {
        $this->hasOne("id", "Sessions", "id", array(
            'alias' => 'Session',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));
    }

Sessions:

    public function initialize()
    {
        $this->belongsTo("id", "Users", "id");
    }

What is supose to do?I



98.9k

What code are you using to save the user/session instances?



25.0k
edited Sep '14

Let's say , when I save Users model it automatically set values and saves the Sessions model.



25.0k
edited Sep '14

I want when i use $users->save($this->request->getPost(), $form), to store the token that come from the form to be stored into the another model Sessions So, how can achieved that?

Here is what i'm doing in the controller:

public function signUpAction()
    {
        if (!empty($this->isAuth())) {
            $this->redirect(IndexEnom::HOME_URL);
        }

        $form = new SignUpForm();
        if ($this->request->isPost()) {

            $users = new Users();
            if ($users->save($this->request->getPost(), $form)) {
                $this->saveUserData($this->request->getPost('email'), $this->request->getPost('password'));
                $this->authAction();
            }

            foreach ($users->getMessages() as $message) {
                $this->flashSession->error($message->getMessage());
            }
        }

        $this->view->pick("index/signup");
        $this->view->form = $form;
    }

User model:

namespace Modules\Engine\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness;
use Modules\Engine\Models\Validators\PasswordValidator;

class Users extends Model
{

    public $id;
    public $email;
    public $password;
    public $username;
    public $type;
    public $token;

    public function validation()
    {
        $this->validate(new PasswordValidator(
                array(
            "field" => "password"
                )
        ));

        $this->validate(new EmailValidator(
                array(
            "field" => "email",
            "message" => "Email is not valid!"
                )
        ));

        $this->validate(new Uniqueness(
                array(
            "field" => "email",
            "message" => "Type another email!"
                )
        ));

        return true !== $this->validationHasFailed();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function setPassword($password)
    {

        $this->password = $this->getDi()->getSecurity()->hash($password);
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getToken()
    {
        return $this->token;
    }

    public function setToken($token)
    {
        $this->token = $this->getDi()->getSecurity()->hash($token);
    }

}

Sessions model:

namespace Modules\Engine\Models;

use Phalcon\Mvc\Model;

class Sessions extends Model
{

    public $id;
    public $auth_token;
    public $activity;

    public function getId()
    {
        return $this->id;
    }

    public function getAuth_token()
    {
        return $this->auth_token;
    }

    public function getActivity()
    {
        return $this->activity;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setAuthToken($auth_token)
    {
        $this->auth_token = $this->getDi()->getSecurity()->hash($auth_token);
    }

    public function setActivity()
    {
        $this->activity = time();
    }

}