/** * Test Model.beforeMarshal event on associated tables. * * @return void */ public function testBeforeMarshalEventOnAssociations() { $data = ['title' => 'My title', 'body' => 'My content', 'author_id' => 1, 'user' => ['username' => 'mark', 'password' => 'secret'], 'comments' => [['comment' => 'First post', 'user_id' => 2], ['comment' => 'Second post', 'user_id' => 2]], 'tags' => [['tag' => 'news', '_joinData' => ['active' => 1]], ['tag' => 'cakephp', '_joinData' => ['active' => 0]]]]; $marshall = new Marshaller($this->articles); $this->articles->users->eventManager()->attach(function ($e, $data) { $data['secret'] = 'h45h3d'; }, 'Model.beforeMarshal'); $this->articles->comments->eventManager()->attach(function ($e, $data) { $data['comment'] .= ' (modified)'; }, 'Model.beforeMarshal'); $this->articles->tags->eventManager()->attach(function ($e, $data) { $data['tag'] .= ' (modified)'; }, 'Model.beforeMarshal'); $this->articles->tags->junction()->eventManager()->attach(function ($e, $data) { $data['modified_by'] = 1; }, 'Model.beforeMarshal'); $entity = $marshall->one($data, ['associated' => ['Users', 'Comments', 'Tags']]); $this->assertEquals('h45h3d', $entity->user->secret); $this->assertEquals('First post (modified)', $entity->comments[0]->comment); $this->assertEquals('Second post (modified)', $entity->comments[1]->comment); $this->assertEquals('news (modified)', $entity->tags[0]->tag); $this->assertEquals('cakephp (modified)', $entity->tags[1]->tag); $this->assertEquals(1, $entity->tags[0]->_joinData->modified_by); $this->assertEquals(1, $entity->tags[1]->_joinData->modified_by); }
/** * Test generating a list of entities from a list of composite ids * * @return void */ public function testOneGenerateBelongsToManyEntitiesFromIds() { $articles = TableRegistry::get('SiteArticles'); $articles->entityClass(__NAMESPACE__ . '\\OpenArticleEntity'); $tags = TableRegistry::get('SiteTags'); $junction = TableRegistry::get('SiteArticlesTags'); $articles->belongsToMany('SiteTags', ['targetTable' => $tags, 'propertyName' => 'tags', 'through' => 'SiteArticlesTags', 'foreignKey' => ['article_id', 'site_id'], 'targetForeignKey' => ['tag_id', 'site_id']]); $data = ['title' => 'Haz tags', 'body' => 'Some content here', 'tags' => ['_ids' => [[1, 1], [2, 2], [3, 1]]]]; $marshall = new Marshaller($articles); $result = $marshall->one($data, ['associated' => ['SiteTags']]); $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]); $data = ['title' => 'Haz tags', 'body' => 'Some content here', 'tags' => ['_ids' => [1, 2, 3]]]; $marshall = new Marshaller($articles); $result = $marshall->one($data, ['associated' => ['SiteTags']]); $this->assertEmpty($result->tags); }
/** * Test merging associations inside _joinData * * @return void */ public function testMergeJoinDataAssociations() { $data = ['title' => 'My title', 'body' => 'My content', 'author_id' => 1, 'tags' => [['id' => 1, 'tag' => 'news', '_joinData' => ['active' => 0, 'user' => ['username' => 'Bill']]], ['id' => 2, 'tag' => 'cakephp', '_joinData' => ['active' => 0]]]]; $articlesTags = TableRegistry::get('ArticlesTags'); $articlesTags->belongsTo('Users'); $options = ['Tags' => ['associated' => ['_joinData' => ['associated' => ['Users']]]]]; $marshall = new Marshaller($this->articles); $entity = $marshall->one($data, $options); $entity->accessible('*', true); $data = ['title' => 'Haz data', 'tags' => [['id' => 1, 'tag' => 'news', '_joinData' => ['foo' => 'bar', 'user' => ['password' => 'secret']]], ['id' => 2, '_joinData' => ['active' => 1, 'foo' => 'baz', 'user' => ['username' => 'ber']]]]]; $tag1 = $entity->tags[0]; $result = $marshall->merge($entity, $data, $options); $this->assertEquals($data['title'], $result->title); $this->assertEquals('My content', $result->body); $this->assertSame($tag1, $entity->tags[0]); $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData); $this->assertEquals('Bill', $entity->tags[0]->_joinData->user->username); $this->assertEquals('secret', $entity->tags[0]->_joinData->user->password); $this->assertEquals('ber', $entity->tags[1]->_joinData->user->username); }
/** * Test merging the _joinData entity for belongstomany associations * while passing a whitelist * * @return void */ public function testMergeJoinDataWithFieldList() { $data = ['title' => 'My title', 'body' => 'My content', 'author_id' => 1, 'tags' => [['id' => 1, 'tag' => 'news', '_joinData' => ['active' => 0]], ['id' => 2, 'tag' => 'cakephp', '_joinData' => ['active' => 0]]]]; $options = ['associated' => ['Tags' => ['associated' => ['_joinData']]]]; $marshall = new Marshaller($this->articles); $entity = $marshall->one($data, $options); $entity->accessible('*', true); $data = ['title' => 'Haz data', 'tags' => [['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar', 'crazy' => 'something']], ['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]]]; $tag1 = $entity->tags[0]; $result = $marshall->merge($entity, $data, ['associated' => ['Tags._joinData' => ['fieldList' => ['foo']]]]); $this->assertEquals($data['title'], $result->title); $this->assertEquals('My content', $result->body); $this->assertSame($tag1, $entity->tags[0]); $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData); $this->assertSame(['active' => 0, 'foo' => 'bar'], $entity->tags[0]->_joinData->toArray()); $this->assertSame(['foo' => 'baz'], $entity->tags[1]->_joinData->toArray()); $this->assertEquals('new tag', $entity->tags[1]->tag); $this->assertTrue($entity->tags[0]->dirty('_joinData')); $this->assertTrue($entity->tags[1]->dirty('_joinData')); }
public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRecordsByPrimaryKey() { $data = ['tags' => [['id' => 1], ['id' => 2]]]; $tags = TableRegistry::get('Tags'); $tags->schema()->dropConstraint('primary'); $tags->primaryKey('id'); $marshall = new Marshaller($this->articles); $result = $marshall->one($data, ['associated' => ['Tags']]); $expected = ['tags' => [['id' => 1, 'name' => 'tag1', 'description' => 'A big description'], ['id' => 2, 'name' => 'tag2', 'description' => 'Another big description']]]; $this->assertEquals($expected, $result->toArray()); }