Doctrine orm 另一个实体中的ZFCUser标识 是否有一种方法可以在另一个实体中获取ZfcUser标识或用户id?

Doctrine orm 另一个实体中的ZFCUser标识 是否有一种方法可以在另一个实体中获取ZfcUser标识或用户id?,doctrine-orm,doctrine,entity,zend-framework2,zfcuser,Doctrine Orm,Doctrine,Entity,Zend Framework2,Zfcuser,我一直在看下面的链接,与servicemanager打交道 使用服务管理器时,我得到以下信息: 对非对象调用成员函数get 在以下行: $auth = $sm->get('zfcuser_auth_service'); $auth = $this->getServiceManager()->get('zfcuser_auth_service'); 使用ServiceManager 在上面的代码中,我一直在尝试使用ServiceManager,但没有任何运气。正如我所说,在这

我一直在看下面的链接,与servicemanager打交道

使用服务管理器时,我得到以下信息:

对非对象调用成员函数get

在以下行:

$auth = $sm->get('zfcuser_auth_service');
$auth = $this->getServiceManager()->get('zfcuser_auth_service');
使用ServiceManager 在上面的代码中,我一直在尝试使用ServiceManager,但没有任何运气。正如我所说,在这一点上出现了问题:

对非对象调用成员函数get

在以下行:

$auth = $sm->get('zfcuser_auth_service');
$auth = $this->getServiceManager()->get('zfcuser_auth_service');
以及ServiceManager接口没有检索ServiceManager的Get方法。因此,在这种情况下,我也尝试使用ServiceLocator而不是Manager,但也没有任何运气

旧版本 我已经能够通过在实体中创建set方法来设置控制器的用户id

控制器:

实体:

新版本 与每次执行操作时都设置setCreator或setModifier不同,在持久化或更新时获取用户标识将是一件好事

我希望在我的实体中有zfcUser_id。下面是一些代码示例,向您展示我的想法:

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class CustomEntity
{
/**
 * @ORM\Column(type="integer", nullable=false)
 */
protected $creator_id;

/**
 *
 * @ORM\Column(type="datetime", nullable=false)
 */
protected $creation_date;

...

/**
 * @ORM\PrePersist
 */
public function onPrePersist()
{
    $this->creator_id = ZfcUser_id
    $this->creation_date = new \DateTime('now');
}

/**
 * @ORM\PreUpdate
 */
public function onUpdate()
{
    $this->last_modifier_id = ZfcUser_id
    $this->last_modified_date = new \DateTime('now');
}
如您所见,我希望在以下函数中使用ZfcUser_id:onPrePersist和onUpdate来保存此实体/对象的创建者或更新者。对于这个版本,我正在寻找获取ZfcUser身份的方法,并找到以下链接:

基于答复的结果
创建实体,为字段/属性创建一些设置方法。让您的控制器处理此部分。对于设置为默认值的日期时间,您可以使用onPrePresist、onUpdate,或者您应该查看其他条令块批注。

您希望在哪里访问此信息?回答你的问题有很多可能的方法,你需要更具体一点。@Sam我用一个代码示例编辑了我的问题。嗯,考虑到教义是如何运作的,我认为不可能这样。我认为这项任务是一项手动操作,您可以在您的服务级别执行。记住,实体最好是哑数据对象或POPO!获取当前身份验证等业务逻辑不是实体的问题,而是服务的问题。我同意实体应该是哑数据对象。在旧版本中,我在上面的问题中添加了,在刷新实体之前,我已经设置了实体的创建者和修饰符。你觉得这样解决怎么样,所以实体除了保存数据之外什么都不做。在prePersist/Update上设置日期和时间是可以的。如您所指出的,以旧的方式设置身份将是首选。
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class CustomEntity
{
/**
 * @ORM\Column(type="integer", nullable=false)
 */
protected $creator_id;

/**
 *
 * @ORM\Column(type="datetime", nullable=false)
 */
protected $creation_date;

...

/**
 * @ORM\PrePersist
 */
public function onPrePersist()
{
    $this->creator_id = ZfcUser_id
    $this->creation_date = new \DateTime('now');
}

/**
 * @ORM\PreUpdate
 */
public function onUpdate()
{
    $this->last_modifier_id = ZfcUser_id
    $this->last_modified_date = new \DateTime('now');
}