Re: [Modeling-users] self.getId()
Status: Abandoned
Brought to you by:
sbigaret
|
From: Sebastien B. <sbi...@us...> - 2003-03-17 23:33:12
|
so...@la... wrote:
> On Mon, Mar 17, 2003 at 07:24:04PM +0100, so...@la... wrote:
> >=20
> >=20
> > I get one question:=20
> > how could i get the id for an object (I ever see this=20
> > on the list, but i miss it). cause while working on=20
> > web using real objects (read between two transaction)
> > is impossible without using user session, so i think=20
> > working on id is a nice way to solve this for me.=20
> > I know that working on id is a bad habit but ..=20
>=20
> Answering myself. The doc says :
> "you shouldn't need to expose the PK values as class' attributes. But,
> ok, if you really want to do that, that is to say, if you declare them
> as class properties, "=20
>=20
> That 's what i did, but this is wrong too, let read this=20
> snapshot:
>=20
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> from mortal.Objects.Article import Article
> from Modeling.EditingContext import EditingContext
> from Modeling.FetchSpecification import FetchSpecification
>=20
> # creating a new object
> ec =3D EditingContext()
> article =3D Article()
> article.setText('Oups')
> article.setTitle('Again')
> ec.insertObject(article)
> ec.saveChanges()
>=20
> # fetching the last inserted=20
> fetchSpec=3DFetchSpecification(entityName=3D'Article')
> ec.objectsWithFetchSpecification(fetchSpec)[-1]
> <mortal.Objects.Article.Article instance at 0x85cfd44>
> ec.objectsWithFetchSpecification(fetchSpec)[-1].getId()
> # <---> the id is 0 cause the fetch isn't done here.
> 0=20
> ec.dispose()
> # <--> after a dispose it's work .=20
> ec.objectsWithFetchSpecification(fetchSpec)[-1].getId()
> 26L
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
That is really strange. I just made the very same thing with one of the
framework's testPackage and everything seems all right:
>>> from Modeling.EditingContext import EditingContext
>>> from Modeling.FetchSpecification import FetchSpecification
>>> from testPackages.AuthorBooks.Book import Book
>>> b=3DBook()
>>> b.setTitle('blah')
>>> ec=3DEditingContext()
>>> ec.insertObject(b)
>>> ec.saveChanges()
>>> ec.globalIDForObject(b).keyValues()['id']
63L
...wait... Oh, ok, now I see:
>>> from Modeling.EditingContext import EditingContext
>>> from Modeling.FetchSpecification import FetchSpecification
>>> from testPackages.AuthorBooks.Book import Book
>>> b=3DBook()
>>> b.setTitle('blah')
>>> ec=3DEditingContext()
>>> ec.insertObject(b)
>>> ec.saveChanges()
>>> ec.globalIDForObject(b).keyValues()['id']
64L
>>> b._id
0
You are using getId() (returning article._id) instead of getID()
(returning the PK value stored in its GlobalID) !
--> using getID() will probably make it.
However, we have a bug here: Article's PK did not get its value.
> 2) for an unknow reason right now, (perhaps i'll debug that)=20
> even calling dispose() on my ec don't force a re-fecht
> in a real application
I must check that, this is not a normal behaviour at all. article._id
should have received its value. I'll check that. Seems you find one
again ;)
-- S=E9bastien.
|