muck_fuppet
muck_fuppet

Reputation: 69

Symfony __PHP_Incomplete_Class could not be converted to string

When I submit my login form I get that error. I read here that this could be caused by trying to

deserialze object without loaded class for that object

however because the code is hidden away within symfony internals I can't work out why a class wouldn't be loaded in that instance. Profiler seems to say that no queries have been performed, yet if I put in the wrong credentials it says "invalid credentials" so it must be accessing the DB correctly.

User class

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity("email", message="This email is already in use.")
* @UniqueEntity("username", message="This username is already in use")
*/
class User implements UserInterface, \Serializable
{

private $roles = "ROLE_USER";

/**
 * @ORM\Column(name="salt",type="string", length=255)
 */
private $salt = "saltyboye";


/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(name="username",type="string", length=255, unique=true)
 */
private $username;

/**
 * @ORM\Column(name = "password", type="string", length=255)
 */
private $password;

/**
 * @ORM\Column(name="email", type="string", length=255, unique=true)
 */
private $email;

/**
 * @ORM\Column(type="datetime")
 */
private $registeredOn;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
private $referrer;

/**
 * @ORM\Column(type="smallint")
 */
private $entries;

/**
 * @ORM\Column(type="string", length=3)
 */
private $currency;


 /** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->registeredOn,
        $this->id,
        $this->email,
        $this->username,
        $this->password,
        $this->roles,
        $this->referrer,
        $this->currency,
        $this->entries,
        $this->salt));
}

public function unserialize($serialized)
{
    list (
        $this->id,
        $this->email,
        $this->username,
        $this->password,
        $this->roles,
        $this->referrer,
        $this->currency,
        $this->entries,
        $this->salt) = unserialize($serialized, array('allowed_classes' => false));
}
public function eraseCredentials()
{

}
public function getRoles()
{
    return array("ROLE_USER");
}


public function getSalt()
{
    return $this->salt;
}


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

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

public function setUsername(string $username): self
{
    $this->username = $username;

    return $this;
}

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

public function setPassword(string $password): self
{
    $this->password = $password;

    return $this;
}

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

public function setEmail(string $email): self
{
    $this->email = $email;

    return $this;
}

public function getRegisteredOn(): ?\DateTimeInterface
{
    return $this->registeredOn;
}

public function setRegisteredOn(\DateTimeInterface $registeredOn): self
{
    $this->registeredOn = $registeredOn;

    return $this;
}

public function getReferrer(): ?int
{
    return $this->referrer;
}

public function setReferrer(?int $referrer): self
{
    $this->referrer = $referrer;

    return $this;
}

public function getEntries(): ?smallint
{
    return $this->entries;
}

public function setEntries($entries): self
{
    $this->entries = $entries;

    return $this;
}

public function setCurrency($currency): self
{
    $this->currency = $currency;

    return $this;
}
public function getCurrency(): ?string
{
    return $this->currency;
}

}

Security.yaml

security:
hide_user_not_found: false
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
    App\Entity\User: sha256
providers:
    in_memory: { memory: ~ }
    main_db_provider:
        entity:
            class: App\Entity\User
            property: username

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        # anonymous: true
        pattern: ^/ #test
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
            csrf_token_generator: security.csrf.token_manager
            provider: main_db_provider

        # activate different ways to authenticate

        # http_basic: true
        # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

        # form_login: true
        # https://symfony.com/doc/current/security/form_login_setup.html

# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
    - { path: ^/$, roles: ROLE_USER }
    # - { path: ^/$, roles: ROLE_USER }

Upvotes: 3

Views: 1508

Answers (1)

fucethebads
fucethebads

Reputation: 469

The serialize function doesnot match the unserialize function! You need to change it:

/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
    $this->registeredOn,
    $this->id,
    $this->email,
    $this->username,
    $this->password,
    $this->roles,
    $this->referrer,
    $this->currency,
    $this->entries,
    $this->salt));
}
public function unserialize($serialized)
{
   list (
    $this->registeredOn, # <--- look here it is missing
    $this->id,
    $this->email,
    $this->username,
    $this->password,
    $this->roles,
    $this->referrer,
    $this->currency,
    $this->entries,
    $this->salt) = unserialize($serialized, array('allowed_classes' => false));
}

Upvotes: 1

Related Questions