Biting the bullet and doing some playing around with model unit testing.
As a bit of a trial, I've got a beforeSave which searches $this for the max of a field and increments it by 1.public function beforeSave($options = array()) { if (!isset($this->data['number'])) { $lastNumber = $this->find('first', [ 'fields' => ['id', 'max_number'], 'conditions' => ['Issue.project_id' => $this->data['project_id']] ]); $this->data['number'] = ++$lastNumber['Issue']['max_number']; } return true; }
public $virtualFields = array( 'max_number' => 'MAX(number)' );
public function testBeforeSaveShouldIncrementNewIssueNumber() { $this->loadFixtures('Issue', 'Project', 'IssueType'); $this->Issue->data = [ 'project_id' => 1, 'type_id' => 1, 'short_description' => 'lorem ipsum', 'description' => 'Lorem ipsum dolor sit amet', 'created' => '2013-06-07 13:34:20', 'modified' => '2013-06-07 13:34:20', ]; $this->assertTrue($this->Issue->beforeSave()); // assumes the fixture has 10 records for project 1 $this->assertEquals(11, $this->Issue->data['number']); }
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment