0
votes

Update: It's not just username that's this affects. I even tried renaming the column to foo in my schema, and still it always returns null for that column!

My database is in MySQL (InnoDB). I have a users table, containing the properties ID, username, name, email, password. Table has a bunch of users inserted in it.

On the Doctrine side, my AppBundle\Entity\User class is correctly defined. I can query my users and get a list of them, with all the columns intact -- except for the username column.

Despite username being defined in PHP:

/**
 * User
 *
 * @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"})})
 * @ORM\Entity
 */
class User
{
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45,  nullable=false)
     */
    private $username;

    // ...


    /**
     * Set username
     *
     * @param string $username
     *
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

When I get the entities and dump them, the username field has mysteriously been nulled:

User {#389 ▼
  -id: 2
  -username: null
  -name: "Sam"
  -password: XXX
  -email: XXX
  -oPhone: ""
  -mPhone: XXX
  -hPhone: ""
  -institutions: PersistentCollection {#390 ▶}
}

Even though it definitely exists and is not null on the database:

mysql> SELECT * FROM user WHERE id=2

# ID, username, name, password, email, o_phone, m_phone, h_phone
2, sjw, Sam, XXX, XXX, , XXX, 

And trying to access the field via PHP results in it complaining that no such username field exists, preventing me from doing user auth using this entity.

  • I'm not using any custom app bundles in Symfony, which is version 2.7.6 (with doctrine2), on PHP 5.4.

  • I've cleared my caches, made sure I haven't had any Doctrine-generated .yml or .xml models lying around.

  • I've sanity-checked my database -- using the sqlalchemy Python library and sqlacodegen I have a python version of my ORM. That has no problems with the username column, results appear as they should, rather than being null or NoneType.

  • I've temporarily disabled the SecurityBundle to see if it was removing the username behind the scenes -- the field is still nulled.

  • I've pored over the Symfony docs...

Nope, out of ideas. What is Symfony doing behind the scenes?

1
change your model to public $username;Dale
"And trying to access the field via PHP" are you using $user->getUsername() when you the error?Cerad
Can't recall what the error was -- when I was trying to use the class for user authentication, the framework was throwing errors about username not being a field (even when it was set to public). That was yesterday's work, however.. :pOwain Jones
add to your question how do you saving userSergio Ivanuzzo
@Dale setting Entity fields to public is a VERY bad idea.Sergio Ivanuzzo

1 Answers

0
votes

So it turns out it was a cache problem, as the chosen answer here explains. I completely forgot that I had enabled APC to cache database entries! Turns out it was also caching the database metadata and various bits of code, so even when I was clearing everything else out, the APC cache didn't get updated.

This was the culprit, these lines in config.yml which should only have been in config_prod.yml:

    doctrine:
        orm:
            metadata_cache_driver: apc
            result_cache_driver: apc
            query_cache_driver: apc

(Another solution might have been to just restart php-fpm, as that's how I'm running PHP in my environment -- something that would have taken a second to do but I didn't think of for a whole two days... doh...)