Primary Keys

38 views
Skip to first unread message

stutteringp0et

unread,
Feb 17, 2011, 10:11:27 AM2/17/11
to YiiMongoDbSuite
Documentation says:
Support for using any other than _id field as a Primary Key, for a
collection

How do I use it?

In my the generated model, there is a function "primaryKey()" that
returns '_id' by default. If I change it to 'id', nothing happens.
Data is still stored with '_id' as a MongoId Object and the 'id' field
is still empty.

canni

unread,
Feb 17, 2011, 10:22:59 AM2/17/11
to YiiMongoDbSuite
You can't change the _id field it will be there always, refer to mongo
documentation

the primaryKey() method allows You to *use* different field like it
would be a primary key (this affects to behavior of ie. findByPk()
methods)
sill You have to remember to define an unique index on field that
You'll be using as a primary key.

And mongo will not pre populate any other field than _id with
automatic MongoId object.

canni

stutteringp0et

unread,
Feb 18, 2011, 1:02:34 AM2/18/11
to YiiMongoDbSuite
I didn't realise that in order to save a value to a field, that field
needs a rule defined in the model.

Mayur Ahir

unread,
Oct 7, 2011, 2:41:59 AM10/7/11
to yiimong...@googlegroups.com
 These is what I am successfully doing..

class MyClass extends EMongoDocument {
public $id; //Primary Key
.
.
public function primaryKey() {
return 'id';
}
.
.
public function beforeSave() {
        if ($this->isNewRecord) {
$this->id = uniqid(); // OR $this->id = sha1(uniqid(rand(), true)) //Highly unique if you have millions of records
        }
        return parent::beforeSave();
}
.
.
}

Attila Nagy

unread,
Oct 7, 2011, 4:22:23 AM10/7/11
to yiimong...@googlegroups.com
On Fri, Oct 7, 2011 at 8:41 AM, Mayur Ahir <ahir...@gmail.com> wrote:
>         if ($this->isNewRecord) {
> $this->id = uniqid(); // OR $this->id = sha1(uniqid(rand(), true)) //Highly
> unique if you have millions of records
>         }

just a quick question: is it necessary to create a uniqid for your
records? YMDBS automatically sets the _id variable with a new MongoId,
that is designed to be unique among different servers too. So in most
cases you can depend on that variable. Becouse in Mongo you _must_
have an _id field, so in your example you'll have two identifiers (_id
and id) for every document.

--
Nagy Attila Gabor

Mayur Ahir

unread,
Oct 7, 2011, 7:47:06 AM10/7/11
to yiimong...@googlegroups.com
$model->findByPk('4ced75e1eb4ae8ca44000000'); is much simpler than $model->findByPk(new MongoID('4ced75e1eb4ae8ca44000000')); You don't need to do much migration....

Attila Nagy

unread,
Oct 7, 2011, 9:31:32 AM10/7/11
to yiimong...@googlegroups.com

ok, it's your choice. But Mongo let's you use other types than MongoId
for primary key also, so you could just override the beforeSave, to
generate the id's you like.

--
Nagy Attila Gabor

Masaru Hoshi

unread,
Oct 7, 2011, 9:36:16 AM10/7/11
to yiimong...@googlegroups.com
Mayur,

Either you choose using id or _id, MongoDb team recommends using your primary keys as binary whether it's possible as a tunning condition. 

--
Mr. Masaru Hoshi | Senior Software Analyst | ho...@hoshi.com.br

This message may contain confidential and/or privileged information. If you are not the addressee or authorized to receive this for the addressee, you must not use, copy,  disclose or take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by reply e-mail and delete this message. Thank you for your cooperation.

このメッセージは機密および/または特権情報を含むかもしれない。受信人または受信人のためのこれを受け取るために承認されてでなければこのメッセージか情報にここに基づく処置を使用するか、コピーするか、表わすか、またはとってはならない。間違いのこのメッセージを受け取ったら、送り主に応答の電子メールによってすぐ助言し、このメッセージを削除しなさい。あなたの協同をありがとう。
Reply all
Reply to author
Forward
0 new messages