public function beforeSave(Event $event, Entity $entity) { $get_user_id_function = $this->config('get_user_id_function'); if (!empty($get_user_id_function)) { $user_id = call_user_func($get_user_id_function); if ($entity->isNew() && $entity->accessible('created_by')) { $entity->created_by = $user_id; } if (!$entity->isNew() && $entity->accessible('modified_by')) { $entity->modified_by = $user_id; } } }
public function beforeSave(Event $event, Entity $entity) { /* * Save Model only after a commit when the ancestors table has been saved as well * -> start a transaction */ $table = $event->subject(); $connection = $table->connection(); $connection->begin(); /* * Get existing parent_id to check if the node in moved in the tree */ $this->has_new_parent = false; if (!$entity->isNew()) { /* * If the entity is not a new one, it may have been moved in the tree */ $primaryKey = $table->schema()->primaryKey(); $primaryKey = count($primaryKey) == 1 ? $primaryKey[0] : $primaryKey; $parent_id_fieldname = $this->config('model_parent_id_fieldname'); if ($entity->accessible($parent_id_fieldname)) { $parent_id = $entity->{$parent_id_fieldname}; $id = $entity->{$primaryKey}; $existing_entity = $table->get($id); if ($parent_id != $existing_entity->{$parent_id_fieldname}) { /* * Node has been moved in the Tree */ $this->has_new_parent = true; } } else { throw new \Exception(__d('alaxos', 'the parent field is not accesible')); } } return true; }
/** * Test merge with validation and create or update validation rules * * @return void */ public function testMergeWithCreate() { $data = ['title' => 'My title', 'author_id' => 'foo']; $marshall = new Marshaller($this->articles); $entity = new Entity(['title' => 'Foo', 'body' => 'My Content', 'author_id' => 1]); $entity->accessible('*', true); $entity->isNew(true); $entity->clean(); $this->articles->validator()->requirePresence('thing', 'update')->add('author_id', 'numeric', ['rule' => 'numeric', 'on' => 'update']); $expected = clone $entity; $result = $marshall->merge($expected, $data, []); $this->assertEmpty($result->errors('author_id')); $this->assertEmpty($result->errors('thing')); $entity->clean(); $entity->isNew(false); $result = $marshall->merge($entity, $data, []); $this->assertNotEmpty($result->errors('author_id')); $this->assertNotEmpty($result->errors('thing')); }
/** * Tests __debugInfo * * @return void */ public function testDebugInfo() { $entity = new Entity(['foo' => 'bar'], ['markClean' => true]); $entity->somethingElse = 'value'; $entity->accessible('name', true); $entity->virtualProperties(['baz']); $entity->dirty('foo', true); $entity->errors('foo', ['An error']); $entity->source('foos'); $result = $entity->__debugInfo(); $expected = ['foo' => 'bar', 'somethingElse' => 'value', '[new]' => true, '[accessible]' => ['*' => true, 'name' => true], '[dirty]' => ['somethingElse' => true, 'foo' => true], '[original]' => [], '[virtual]' => ['baz'], '[errors]' => ['foo' => ['An error']], '[repository]' => 'foos']; $this->assertSame($expected, $result); }
/** * Tests __debugInfo * * @return void */ public function testDebugInfo() { $entity = new Entity(['foo' => 'bar'], ['markClean' => true]); $entity->accessible('name', true); $entity->virtualProperties(['baz']); $entity->dirty('foo', true); $entity->errors('foo', ['An error']); $entity->source('foos'); $result = $entity->__debugInfo(); $expected = ['new' => true, 'accessible' => ['*' => true, 'name' => true], 'properties' => ['foo' => 'bar'], 'dirty' => ['foo' => true], 'original' => [], 'virtual' => ['baz'], 'errors' => ['foo' => ['An error']], 'repository' => 'foos']; $this->assertSame($expected, $result); }
/** * Tests merging associated data with a fieldList * * @return void */ public function testMergeAssociationWithfieldList() { $user = new Entity(['username' => 'mark', 'password' => 'secret']); $entity = new Entity(['tile' => 'My Title', 'user' => $user]); $user->accessible('*', true); $entity->accessible('*', true); $data = ['body' => 'My Content', 'something' => 'else', 'user' => ['password' => 'not a secret', 'extra' => 'data']]; $marshall = new Marshaller($this->articles); $marshall->merge($entity, $data, ['fieldList' => ['something'], 'associated' => ['Users' => ['fieldList' => ['extra']]]]); $this->assertNull($entity->body); $this->assertEquals('else', $entity->something); $this->assertSame($user, $entity->user); $this->assertEquals('mark', $entity->user->username); $this->assertEquals('secret', $entity->user->password); $this->assertEquals('data', $entity->user->extra); $this->assertTrue($entity->dirty('user')); }
/** * Tests that merging data to an entity containing belongsToMany and _ids * will just overwrite the data * * @return void */ public function testMergeBelongsToManyEntitiesFromIds() { $entity = new Entity(['title' => 'Haz tags', 'body' => 'Some content here', 'tags' => [new Entity(['id' => 1, 'name' => 'Cake']), new Entity(['id' => 2, 'name' => 'PHP'])]]); $data = ['title' => 'Haz moar tags', 'tags' => ['_ids' => [1, 2, 3]]]; $entity->accessible('*', true); $marshall = new Marshaller($this->articles); $result = $marshall->merge($entity, $data, ['Tags']); $this->assertCount(3, $result->tags); $this->assertInstanceOf('Cake\\ORM\\Entity', $result->tags[0]); $this->assertInstanceOf('Cake\\ORM\\Entity', $result->tags[1]); $this->assertInstanceOf('Cake\\ORM\\Entity', $result->tags[2]); }