Reputation: 9420
The MongoDB documentation on Object IDs recommends using custom keys in a certain case:
If your document has a natural primary key that is immutable we recommend you use that in _id instead of the automatically generated ids.
How can I define a simple model object that does exactly this?
Upvotes: 3
Views: 1830
Reputation: 9420
<?php
namespace Acme\HelloWorld\Model;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
*/
class KindWord {
/**
* @MongoDB\Id(strategy="NONE")
* @var string
*/
private $word;
public function getWord() {
return $this->word;
}
public function setWord($word) {
$this->word = $word;
}
}
Just make sure you set $word
before calling persist()
.
Upvotes: 5