Wednesday, September 29, 2010

Re: New model obj doesn't get populated with data after set() and save()?

To be a bit more specific...

I want to test an unchangeable field "name" of my model:

function testShouldNotAcceptNameAsParameter() { // Name is generated
automatically
$o = new Contract();
$o->create();
$o->set($this->getValidDataExcept(array('name' => 'New Name')));
// Load valid data (only substitute "name" with a new value "New
Name")
$data = $o->save(); // Save it!
$this->assertEqual($data['Contract']['name'],
$this->validData['name']); // Compare original value and generated
value (should be the same)
}

I'm quite happy with this test, but I don't really see why I have to
store the saved data in a $data array? I'd rather would do the
following:

...
$o->save();
$this->assertEqual($o->field('name'), $this->validData['name']);

But $o->field(...) doesn't return *any* value...

Thanks for help!

On Sep 29, 4:50 pm, psybear83 <psybea...@gmail.com> wrote:
> Hi all
>
> Why is a new model object not populated with data etc. after creation?
>
>     $o = new Contract();
>     $o->create();
>     $o->set($this->getValidDataExcept(array('number' =>
> '555-66-777')));
>     $o->save(); // The save works - an array with fields' data is
> returned
>     debug($o->field('number')); // But now the $o seems to be not
> populated at all anymore!
>
> Do I have to manually pull the object again from the database and
> can't just use the newly created one?
>
> Thanks for help
> Josh

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: