Hours with "Models\Users could not be converted to string in "

Hello,

I have tried so much now, even setup an artist/album example from the docs, but that throw the same error. "Models\Users could not be converted to string in"

If someone could help me, i would be forever thankfull.

In my controller:

//Email do not exist in database //Creat new user

            $email = $user->getEmail();

            //generating password
            $random = new \Phalcon\Security\Random();
            $password = $random->base64(12);

            $newUser = new Users([
                'firstName' => $user->getFirstName(),
                'lastName' => $user->getLastName(),
                'email' => $email,
                'password' => $this->security->hash($password),
                'mustChangePassword' => 'Y',
                'active'  => 'Y'
            ]);


            $newFacebook = new Facebook([
                'usersId'    => $newUser,
                'facebookId' => $user->getId(),
                'accessToken' => $accessToken->getValue(),
                'accessTokenExpire' => get_object_vars($accessToken->getexpiresAt())['date']
            ]);

            if ($newFacebook->save()) {
                //Everything is ok, login the new user
                $this->auth->authUserById($newFacebook->user->id);
                $this->response->redirect();
                return false;
            }


            //If something went wrong in the saving prosess
            foreach ($newFacebook->getMessages() as $type => $message) {
                $this->flash->error($message);
            }

            $this->response->redirect('session/signup');
            return false;

Users:

class Users extends \Phalcon\Mvc\Model {

/**
 *
 * @var integer
 * @Primary
 * @Identity
 * @Column(type="integer", length=10, nullable=false)
 */
public $id;

/**
 *
 * @var string
 * @Column(type="string", length=255, nullable=false)
 */
public $firstName;

/**
 *
 * @var string
 * @Column(type="string", length=256, nullable=false)
 */
public $lastName;

/**
 *
 * @var string
 * @Column(type="string", length=255, nullable=false)
 */
public $email;

/**
 *
 * @var string
 * @Column(type="string", length=60, nullable=false)
 */
public $password;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=true)
 */
public $mustChangePassword;

/**
 *
 * @var integer
 * @Column(type="integer", length=10, nullable=false)
 */
public $profilesId;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=false)
 */
public $banned;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=false)
 */
public $suspended;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=true)
 */
public $active;

public $regDate;

/**
 * Validations and business logic
 *
 * @return boolean
 */
public function validation()
{
    $validator = new Validation();

    $validator->add('email', new Uniqueness([
        "message" => "The email is already registered"
    ]));

    return $this->validate($validator);
}


public function initialize()
{
    $this->belongsTo('profilesId', __NAMESPACE__ . '\Profiles', 'id', [
        'alias' => 'profile',
        'reusable' => true
    ]);


    $this->hasOne('id', __NAMESPACE__ . '\Facebook', 'usersId', [
        'alias' => 'facebook'
    ]);


    $this->hasMany('id', __NAMESPACE__ . '\FailedLogins',   'usersId', ['alias' => 'FailedLogins']);
    $this->hasMany('id', __NAMESPACE__ . '\RememberTokens', 'usersId', ['alias' => 'RememberTokens']);
    $this->hasMany('id', __NAMESPACE__ . '\SuccessLogins',  'usersId', ['alias' => 'SuccessLogins']);
    $this->hasMany('id', __NAMESPACE__ . '\ResetPasswords', 'usersId', ['alias' => 'ResetPasswords']);

}


/**
 * Returns table name mapped in the model.
 *
 * @return string
 */
public function getSource()
{
    return 'users';
}

/**
 * Allows to query a set of records that match the specified conditions
 *
 * @param mixed $parameters
 * @return Users[]|Users
 */
public static function find($parameters = null)
{
    return parent::find($parameters);
}

/**
 * Allows to query the first record that match the specified conditions
 *
 * @param mixed $parameters
 * @return Users
 */
public static function findFirst($parameters = null)
{
    return parent::findFirst($parameters);
}

//TODO: Sette opp email confirmation, denne sendes fra model

 /**
 * Before create the user assign a password
 */
public function beforeValidationOnCreate()
{
    // The account must be confirmed via e-mail
    // Only require this if emails are turned on in the config, otherwise account is automatically active
    if(!$this->active){
      if ( $this->getDI()->get('config')->application->useMail) {
          $this->active = 'N';
      } else {
          $this->active = 'Y';
      }
    }

    // The user must not change its password in first login
    if(!$this->mustChangePassword)
    {
        $this->mustChangePassword = 'N';
    }

    if(!$this->suspended)
    {
        // The account is not suspended by default
        $this->suspended = 'N';
    }

    if(!$this->banned)
    {
        // The account is not banned by default
        $this->banned = 'N';
    }

    if(!$this->profilesId)
    {
        // The users gets profile id 2 by default
        $this->profilesId = 2;
    }

    if(!$this->regDate)
    {
        // The users gets profile id 2 by default
        $this->regDate = date('Y-m-d H:i:s');
    }

}

/**
 * Send a confirmation e-mail to the user if the account is not active
 */
public function afterSave()
{
    // Only send the confirmation email if emails are turned on in the config
    //TODO: Update this, setup email support
    if ($this->getDI()->get('config')->application->useMail) {

        if ($this->active == 'N') {

            $emailConfirmation = new EmailConfirmations();

            $emailConfirmation->usersId = $this->id;

            if ($emailConfirmation->save()) {
                $this->getDI()
                    ->getFlash()
                    ->notice('A confirmation mail has been sent to ' . $this->email);
            }
        }
    }
}

}

And facebook:

class Facebook extends \Phalcon\Mvc\Model {

/**
 *
 * @var integer
 * @Primary
 * @Identity
 * @Column(type="integer", length=11, nullable=false)
 */
public $id;

/**
 *
 * @var integer
 * @Column(type="integer", length=10, nullable=false)
 */
public $usersId;

/**
 *
 * @var string
 * @Column(type="string", length=128, nullable=false)
 */
public $facebookId;

/**
 *
 * @var string
 * @Column(type="string", length=512, nullable=false)
 */
public $accessToken;

/**
 *
 * @var string
 * @Column(type="string", nullable=false)
 */
public $accessTokenExpire;

/**
 * Initialize method for model.
 */
public function initialize()
{
  $this->belongsTo('usersId', __NAMESPACE__ . '\Users', 'id', [
      'alias' => 'user',

  ]);
}

/**
 * Returns table name mapped in the model.
 *
 * @return string
 */
public function getSource()
{
    return 'facebook';
}

/**
 * Allows to query a set of records that match the specified conditions
 *
 * @param mixed $parameters
 * @return Facebook[]|Facebook
 */
public static function find($parameters = null)
{
    return parent::find($parameters);
}

/**
 * Allows to query the first record that match the specified conditions
 *
 * @param mixed $parameters
 * @return Facebook
 */
public static function findFirst($parameters = null)
{
    return parent::findFirst($parameters);
}

}



2.7k

What is after "to string in"? In what, where?



2.9k
edited Jul '17

You try to store an object in a intger : 'usersId' => $newUser,

$newFacebook = new Facebook([
                'usersId'    => $newUser,
                'facebookId' => $user->getId(),
                'accessToken' => $accessToken->getValue(),
                'accessTokenExpire' => get_object_vars($accessToken->getexpiresAt())['date']
            ]);

and after facebookId is a string and you store an integer



1.1k

Yeah, i know that.

Storing these record separate works fine But Phalcon should figure out the relationship between the table, and fill in the usersId from the other model object. I cant get that to work.

A suggestion to the documentation is to write a little bit more about this with examples.

You try to store an object in a intger : 'usersId' => $newUser,

```php

$newFacebook = new Facebook([ 'usersId' => $newUser, 'facebookId' => $user->getId(), 'accessToken' => $accessToken->getValue(), 'accessTokenExpire' => getobjectvars($accessToken->getexpiresAt())['date'] ]);

```

and after facebookId is a string and you store an integer



2.9k
edited Jul '17

'usersId' => $newUser will never work and this is the cause of your error message

just do this

usersId'    => $user->getId(),
'facebookId' => '<string with facebook id >',

The relationship between the two models will do the rest automatically

After you can use $newFacebook->getUser()->getFirstName()