我在想,如果有,我可以初始化FOSUserBundle的實體用戶的業主的方式,使其包含誰創造了郵政Symfony的3獲得當前用戶內部實體
我想這樣做的用戶在構造函數內部如下所示。
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="post")
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
/* here are defined some attributs */
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="posts")
* @ORM\JoinColumn(name="owner", referencedColumnName="id")
*/
private $owner;
public function __construct()
{
$this->owner = /* get current user */ ;
}
}
有沒有辦法通過用構造函數替換註釋來做到這一點?
謝謝您的回答
主義實際上並未使用的構造函數,因此沒有。爲什麼不直接在Post和User之間設置關係? – Cerad
關係已經存在,當創建一個新帖子時,所有者屬性將使用當前連接的用戶進行初始化 – paris93
因此,您只需在首次創建新發布時初始化所有者?如果是這樣,下面的兩個答案都是有效的。就我個人而言,我會採用工廠方法,可能會將創建方法添加到發佈存儲庫而不是獨立類。但是列出的所有方法都是有意義的。 – Cerad