Symfony2와 함께 Doctrine Mongo db bundle을 사용합니다. Doctrine Mongodb 문서의 문자열, int 등 데이터 유형에 대한 정보. 그러나 개체 데이터 형식을 찾을 수 없습니다.Doctrine을 사용하여 MongoDB에 개체를 어떻게 추가합니까?
질문 : Doctrine을 사용하여 MongoDB에 개체를 추가하려면 어떻게해야합니까? 문서 클래스에서 (객체 유형)을 어떻게 정의합니까?
Symfony2와 함께 Doctrine Mongo db bundle을 사용합니다. Doctrine Mongodb 문서의 문자열, int 등 데이터 유형에 대한 정보. 그러나 개체 데이터 형식을 찾을 수 없습니다.Doctrine을 사용하여 MongoDB에 개체를 어떻게 추가합니까?
질문 : Doctrine을 사용하여 MongoDB에 개체를 추가하려면 어떻게해야합니까? 문서 클래스에서 (객체 유형)을 어떻게 정의합니까?
"Object"는 문서 (mongodb) 또는 해시 (javascript) 또는 다른 말로 키 - 값 배열을 의미한다고 가정하고 doctrine-mongo docs의 필드 유형 해시를 참조하십시오.
/**
* @Field(type="hash")
*/
protected $yourvariable;
당신은 단순히 @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);
더 나은 읽은 완전한 이해를위한 문서 :
유형의 Symfony2 DoctrineMongoDBBundle page on Symfony website.
thnx을 가지고, 내가 볼 것이다. – user2993220