find()->where(['id' => 1])->first(); $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created); $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated); } /** * Tests that EagerLoader does not try to create queries for associations having no * keys to compare against * * @return void */ public function testEagerLoadingFromEmptyResults() { $table = TableRegistry::get('Articles'); $table->belongsToMany('ArticlesTags'); $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray(); $this->assertEmpty($results); } /** * Tests that duplicate aliases in contain() can be used, even when they would * naturally be attached to the query instead of eagerly loaded. What should * happen here is that One of the duplicates will be changed to be loaded using * an extra query, but yielding the same results * * @return void */ public function testDuplicateAttachableAliases() { TableRegistry::get('Stuff', ['table' => 'tags']); TableRegistry::get('Things', ['table' => 'articles_tags']); $table = TableRegistry::get('Articles'); $table->belongsTo('Authors'); $table->hasOne('Things', ['propertyName' => 'articles_tag']); $table->Authors->target()->hasOne('Stuff', [ 'foreignKey' => 'id', 'propertyName' => 'favorite_tag' ]); $table->Things->target()->belongsTo('Stuff', [ 'foreignKey' => 'tag_id', 'propertyName' => 'foo'] ); $results = $table->find() ->contain(['Authors.Stuff', 'Things.Stuff']) ->toArray(); $this->assertEquals(1, $results[0]->articles_tag->foo->id); $this->assertEquals(1, $results[0]->author->favorite_tag->id); $this->assertEquals(2, $results[1]->articles_tag->foo->id); $this->assertEquals(1, $results[0]->author->favorite_tag->id); $this->assertEquals(1, $results[2]->articles_tag->foo->id); $this->assertEquals(3, $results[2]->author->favorite_tag->id); $this->assertEquals(3, $results[3]->articles_tag->foo->id); $this->assertEquals(3, $results[3]->author->favorite_tag->id); } /** * Test for https://github.com/cakephp/cakephp/issues/3410 * * @return void */ public function testNullableTimeColumn() { $table = TableRegistry::get('users'); $entity = $table->newEntity(['username' => 'derp', 'created' => null]); $this->assertSame($entity, $table->save($entity)); $this->assertNull($entity->created); } /** * Test for https://github.com/cakephp/cakephp/issues/3626 * * Checks that join data is actually created and not tried to be updated every time * @return void */ public function testCreateJointData() { $articles = TableRegistry::get('Articles'); $articles->belongsToMany('Highlights', [ 'className' => 'TestApp\Model\Table\TagsTable', 'foreignKey' => 'article_id', 'targetForeignKey' => 'tag_id', 'through' => 'SpecialTags' ]); $entity = $articles->get(2); $data = [ 'id' => 2, 'highlights' => [ [ 'name' => 'New Special Tag', '_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00'] ] ] ]; $entity = $articles->patchEntity($entity, $data, ['Highlights' => ['associated' => ['_joinData']]]); $articles->save($entity); $entity = $articles->get(2, ['contain' => ['Highlights']]); $this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id); $this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d')); } /** * Tests that the junction table instance taken from both sides of a belongsToMany * relationship is actually the same object. * * @return void */ public function testReciprocalBelongsToMany() { $articles = TableRegistry::get('Articles'); $tags = TableRegistry::get('Tags'); $articles->belongsToMany('Tags'); $tags->belongsToMany('Articles'); $left = $articles->Tags->junction(); $right = $tags->Articles->junction(); $this->assertSame($left, $right); } /** * Test for https://github.com/cakephp/cakephp/issues/3677 * * Checks that only relevant associations are passed when saving _joinData * Tests that _joinData can also save deeper associations * * @return void */ public function testBelongsToManyDeepSave() { $articles = TableRegistry::get('Articles'); $articles->belongsToMany('Highlights', [ 'className' => 'TestApp\Model\Table\TagsTable', 'foreignKey' => 'article_id', 'targetForeignKey' => 'tag_id', 'through' => 'SpecialTags' ]); $articles->Highlights->junction()->belongsTo('Authors'); $entity = $articles->get(2, ['contain' => ['Highlights']]); $data = [ 'highlights' => [ [ 'name' => 'New Special Tag', '_joinData' => [ 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author' => [ 'name' => 'jose' ] ] ] ] ]; $entity = $articles->patchEntity($entity, $data, [ 'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']]]] ]); $articles->save($entity, [ 'associated' => [ 'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']]]] ] ]); $entity = $articles->get(2, ['contain' => ['SpecialTags.Authors']]); $this->assertEquals('jose', $entity->special_tags[0]->author->name); } }