Beispiel #1
0
// Find finds the record of this type with the specified id.
//  people := NewModel("people")
//  firstPerson := people.Find(1)
//
// Raises events:
//   Model.AfterFind with Args(record)
func (m *Model) Find(id int64) (*Record, os.Error) {

	key := m.NewKeyWithID(id)
	var record *Record = new(Record)

	err := datastore.Get(m.AppEngineContext(), key, datastore.PropertyLoadSaver(record))

	if err == nil {

		// setup the record object
		record.configureRecord(m, key)

		// raise the AfterFind event on the model
		if m.AfterFind.HasCallbacks() {
			m.AfterFind.Trigger(record)
		}

		// return the record
		return record, nil

	}

	return nil, err

}
Beispiel #2
0
// Put saves or updates this record.  Returns nil if successful, otherwise returns the os.Error
// that was retrned by the datastore.
//  record.Put()
//
// Raises events:
//   Model.BeforePut with Args(record)
//   Model.AfterPut with Args(record)
func (r *Record) Put() os.Error {

	// trigger the BeforePut event on the model
	context := r.model.BeforePut.Trigger(r)

	if !context.Cancel {

		newKey, err := datastore.Put(GetAppEngineContext(), r.DatastoreKey(), datastore.PropertyLoadSaver(r))

		if err == nil {

			// update the record
			r.SetDatastoreKey(newKey).SetNeedsPersisting(false)

			// trigger the AfterPut event
			if r.model.AfterPut.HasCallbacks() {
				r.model.AfterPut.TriggerWithContext(context)
			}

			return nil

		}

		return err

	}

	return ErrOperationCancelledByEventCallback

}
func CreatePersistedRecord(t *testing.T, model *Model) (*Record, os.Error) {

	context := GetAppEngineContext()
	person := model.New()
	key := person.DatastoreKey()

	person.
		SetString("name", "Mat").
		SetInt64("age", 29)

	newKey, err := datastore.Put(context, key, datastore.PropertyLoadSaver(person))

	t.Logf(">> Created new record with ID: '%v'", newKey)

	if err != nil {
		return nil, err
	}

	// set the person ID
	person.setID(newKey.IntID())

	return person, nil

}