/** * Ensure that the association collection can be iterated. * * @return void */ public function testAssociationsCanBeIterated() { $belongsTo = new BelongsTo(''); $this->associations->add('Users', $belongsTo); $belongsToMany = new BelongsToMany(''); $this->associations->add('Cart', $belongsToMany); $expected = ['users' => $belongsTo, 'cart' => $belongsToMany]; $result = iterator_to_array($this->associations, true); $this->assertSame($expected, $result); }
/** * Creates a new BelongsToMany association between this table and a target * table. A "belongs to many" association is a M-N relationship. * * Target table can be inferred by its name, which is provided in the * first argument, or you can either pass the class name to be instantiated or * an instance of it directly. * * The options array accept the following keys: * * - className: The class name of the target table object. * - targetTable: An instance of a table object to be used as the target table. * - foreignKey: The name of the field to use as foreign key. * - targetForeignKey: The name of the field to use as the target foreign key. * - joinTable: The name of the table representing the link between the two * - through: If you choose to use an already instantiated link table, set this * key to a configured Table instance containing associations to both the source * and target tables in this association. * - cascadeCallbacks: Set to true if you want CakePHP to fire callbacks on * cascaded deletes. If false the ORM will use deleteAll() to remove data. * When true join/junction table records will be loaded and then deleted. * - conditions: array with a list of conditions to filter the join with. * - sort: The order in which results for this association should be returned. * - strategy: The strategy to be used for selecting results Either 'select' * or 'subquery'. If subquery is selected the query used to return results * in the source table will be used as conditions for getting rows in the * target table. * - saveStrategy: Either 'append' or 'replace'. Indicates the mode to be used * for saving associated entities. The former will only create new links * between both side of the relation and the latter will do a wipe and * replace to create the links between the passed entities when saving. * * This method will return the association object that was built. * * @param string $associated the alias for the target table. This is used to * uniquely identify the association * @param array $options list of options to configure the association definition * @return \Cake\ORM\Association\BelongsToMany */ public function belongsToMany($associated, array $options = []) { $options += ['sourceTable' => $this]; $association = new BelongsToMany($associated, $options); return $this->_associations->add($association->name(), $association); }
/** * Integration test for replacing entities which depend on their source entity with HasMany and failing transaction. False should be returned when * unlinking fails while replacing even when cascadeCallbacks is enabled * * @return void */ public function testReplaceHasManyOnErrorDependentCascadeCallbacks() { $articles = $this->getMock('Cake\\ORM\\Table', ['delete'], [['connection' => $this->connection, 'alias' => 'Articles', 'table' => 'articles']]); $articles->method('delete')->willReturn(false); $associations = new AssociationCollection(); $hasManyArticles = $this->getMock('Cake\\ORM\\Association\\HasMany', ['target'], ['articles', ['target' => $articles, 'foreignKey' => 'author_id', 'dependent' => true, 'cascadeCallbacks' => true]]); $hasManyArticles->method('target')->willReturn($articles); $associations->add('articles', $hasManyArticles); $authors = new Table(['connection' => $this->connection, 'alias' => 'Authors', 'table' => 'authors', 'associations' => $associations]); $authors->Articles->source($authors); $author = $authors->newEntity(['name' => 'mylux']); $author = $authors->save($author); $newArticles = $articles->newEntities([['title' => 'New bakery next corner', 'body' => 'They sell tastefull cakes'], ['title' => 'Spicy cake recipe', 'body' => 'chocolate and peppers']]); $sizeArticles = count($newArticles); $this->assertTrue($authors->Articles->link($author, $newArticles)); $this->assertEquals($authors->Articles->findAllByAuthorId($author->id)->count(), $sizeArticles); $this->assertEquals(count($author->articles), $sizeArticles); $newArticles = array_merge($author->articles, $articles->newEntities([['title' => 'Cheese cake recipe', 'body' => 'The secrets of mixing salt and sugar'], ['title' => 'Not another piece of cake', 'body' => 'This is the best']])); unset($newArticles[0]); $this->assertFalse($authors->Articles->replace($author, $newArticles)); $this->assertCount($sizeArticles, $authors->Articles->findAllByAuthorId($author->id)); }