【问题标题】:Doctrine2 after entity update still generate old database table实体更新后的 Doctrine2 仍然生成旧的数据库表
【发布时间】:2014-10-14 07:33:34
【问题描述】:

我正在学习教义2。问题是:我刚刚更新了我的实体类。旧版本的实体由 $id、$name 和 $username 字段组成。在下面的此更新之后,我运行命令教义:生成:实体 Acme、教义:更新:模式等,但结果仍然是只有 3 个字段的旧表。看起来旧的元数据保存在某个地方。有人可以向我提供我做错了什么的信息吗?为什么我得到旧的数据库表而不是新的?甚至如何解决我的问题?

namespace Acme\DemoBundle\Entity;

use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User implements UserInterface, EquatableInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=50)
     */
    private $username;

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

    /**
     * @var string
     *
     * @ORM\Column(name="roles", type="string", length=50)
     */
    private $roles;

    /**
     * @var array
     *
     * @ORM\Column(name="apikey", type="array")
     */
    private $apiKey;

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

    function __construct($apiKey, $id, $password ,$roles , $salt, $username)
    {
        $this->apiKey = $apiKey;
        $this->id = $id;
        $this->password = $password;
        $this->roles = $roles;
        $this->salt = $salt;
        $this->username = $username;
    }

    /**
     * The equality comparison should neither be done by referential equality
     * nor by comparing identities (i.e. getId() === getId()).
     *
     * However, you do not need to compare every attribute, but only those that
     * are relevant for assessing whether re-authentication is required.
     *
     * Also implementation should consider that $user instance may implement
     * the extended user interface `AdvancedUserInterface`.
     *
     * @param UserInterface $user
     *
     * @return bool
     */
    public function isEqualTo(UserInterface $user)
    {

        if (!$user instanceof User) {
            return false;
        }

        if ($this->password !== $user->getPassword()) {
            return false;
        }

        if ($this->salt !== $user->getSalt()) {
            return false;
        }

        if ($this->username !== $user->getUsername()) {
            return false;
        }

        return true;
    }



    /**
     * Returns the roles granted to the user.
     *
     * <code>
     * public function getRoles()
     * {
     *     return array('ROLE_USER');
     * }
     * </code>
     *
     * Alternatively, the roles might be stored on a ``roles`` property,
     * and populated in any number of different ways when the user object
     * is created.
     *
     * @return Role[] The user roles
     */
    public function getRoles()
    {
        return $this->roles;
    }

    /**
     * Returns the password used to authenticate the user.
     *
     * This should be the encoded password. On authentication, a plain-text
     * password will be salted, encoded, and then compared to this value.
     *
     * @return string The password
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Returns the salt that was originally used to encode the password.
     *
     * This can return null if the password was not encoded using a salt.
     *
     * @return string|null The salt
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Returns the username used to authenticate the user.
     *
     * @return string The username
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @return string
     */
    public function getApiKey()
    {
        return $this->apiKey;
    }

    /**
     * @param string $apiKey
     */
    public function setApiKey($apiKey)
    {
        $this->apiKey = $apiKey;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }


    /**
     * Removes sensitive data from the user.
     *
     * This is important if, at any given point, sensitive information like
     * the plain-text password is stored on this object.
     */
    public function eraseCredentials()
    {
        // TODO: Implement eraseCredentials() method.
    }
}

【问题讨论】:

  • doctrine:generate:entities 将读取数据库结构并从信息中生成实体。你到底在做什么?
  • 学说:模式:创建,学说:模式:更新。我删除旧表和数据库然后创建新的等等。但结果总是一样的。我成功地做了教义:生成:实体,但仍然没有什么新鲜事。更准确地说,我删除了旧数据库,然后尝试所有变体来创建新表。
  • php_errors.log 中有什么有用的提示吗?
  • 没有有用的提示。在我看来,有某种文件可以从中获取旧的元数据。我找不到它。尝试了所有的学说缓存删除命令。

标签: symfony doctrine-orm


【解决方案1】:

如果保存了旧的元数据你必须清除缓存

doctrine:cache:clear-metadata         Clears all metadata cache for an entity manager
  doctrine:cache:clear-query            Clears all query cache for an entity manager
  doctrine:cache:clear-result           Clears result cache for an entity manager

【讨论】:

    【解决方案2】:

    我认为你没有正确执行更新命令。

    您需要强制更改:

    php app/console doctrine:schema:update --force
    

    或者转储 SQL 并手动执行:

    php app/console doctrine:schema:update --dump-sql
    

    【讨论】:

    • 我知道这听起来很奇怪很有趣,但它不起作用。强制更新和 sql 转储后,我得到旧的 sql 查询。我可能打破了一些我无法理解的 atm 的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多