2013-12-23 6 views
0

Symfony2와 함께 Doctrine Mongo db bundle을 사용합니다. Doctrine Mongodb 문서의 문자열, int 등 데이터 유형에 대한 정보. 그러나 개체 데이터 형식을 찾을 수 없습니다.Doctrine을 사용하여 MongoDB에 개체를 어떻게 추가합니까?

질문 : Doctrine을 사용하여 MongoDB에 개체를 추가하려면 어떻게해야합니까? 문서 클래스에서 (객체 유형)을 어떻게 정의합니까?

답변

2

당신은 단순히 @MongoDB \ 문서 주석을 가진 클래스 정의 :

<?php 

namespace Radsphere\MissionBundle\Document; 
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; 
/** 
* @MongoDB\Document(
*  collection="user_statistics", 
*  repositoryClass="Radsphere\MissionBundle\DocumentRepository\UserStatisticsRepository", 
*  indexes={ 
*   @MongoDB\Index(keys={"user_profile_id"="asc"}) 
*  } 
* ) 
*/ 
class UserStatistics 
{ 


/** 
    * @var \MongoId 
    * 
    * @MongoDB\Id(strategy="AUTO") 
    */ 
protected $id; 
/** 
    * @var string 
    * 
    * @MongoDB\Field(name="user_profile_id", type="int") 
    */ 
protected $userProfileId; 
/** 
    * @var integer 
    * 
    * @MongoDB\Field(name="total_missions", type="int") 
    */ 
protected $totalMissions; 
/** 
    * @var \DateTime 
    * 
    * @MongoDB\Field(name="issued_date", type="date") 
    */ 
    protected $issuedDate; 

    /** 
    * 
    */ 
    public function __construct() 
    { 
    $this->issuedDate = new \DateTime(); 
    } 

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

    /** 
    * {@inheritDoc} 
    */ 
    public function getIssuedDate() 
    { 
    return $this->issuedDate; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public function setIssuedDate($issuedDate) 
    { 
    $this->issuedDate = $issuedDate; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public function getTotalMissions() 
    { 
    return $this->totalMissions; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public function setTotalMissions($totalMissions) 
    { 
    $this->totalMissions = $totalMissions; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public function getUserProfileId() 
    { 
    return $this->userProfileId; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public function setUserProfileId($userProfileId) 
    { 
    $this->userProfileId = $userProfileId; 
    } 

} 

그런 다음 문서 사용 문서 관리자를 만들기 위해를 :

$userStatisticsDocument = new UserStatistics(); 
    $userStatisticsDocument->setUserProfileId($userProfile->getId()); 

    $userStatisticsDocument->setTotalMissions($totalMissions); 
    $userStatisticsDocument->setIssuedDate(new \DateTime('now')); 
    $this->documentManager->persist($userStatisticsDocument); 
    $this->documentManager->flush($userStatisticsDocument);