【问题标题】:GetId doesn't workGetId 不起作用
【发布时间】:2013-02-26 10:03:54
【问题描述】:

我在 Symfony2 中使用 FosUserBundle 运行以下代码:

var_dump($myUser->getId())

但它什么也不返回,而 var_dump($myUser->getUsername()) 有效。为什么getId() 不起作用?

我的 user.php 中的吸气剂:

public function getId()
{
    return parent::getId();
}

【问题讨论】:

  • 你能发布你的用户类吗?你也覆盖父 id 属性吗?

标签: php symfony fosuserbundle


【解决方案1】:

根据https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md,您的用户类应该是这样的。

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

你不需要打电话

return parent::getId();

有了上面的类

return $this->id;

应该可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多