/**
  * 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);
 }
Example #2
0
 /**
  * Returns an array that can be used to describe the internal state of this
  * object.
  *
  * @return array
  */
 public function __debugInfo()
 {
     $conn = $this->connection();
     return ['registryAlias' => $this->registryAlias(), 'table' => $this->table(), 'alias' => $this->alias(), 'entityClass' => $this->entityClass(), 'associations' => $this->_associations->keys(), 'behaviors' => $this->_behaviors->loaded(), 'defaultConnection' => $this->defaultConnectionName(), 'connectionName' => $conn ? $conn->configName() : null];
 }
Example #3
0
 /**
  * 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));
 }
Example #4
0
 /**
  * {@inheritDoc}
  *
  * Those entries in `$entities` that cannot be matched to any record in
  * `$data` will be discarded. Records in `$data` that could not be matched will
  * be marshalled as a new entity.
  *
  * When merging HasMany or BelongsToMany associations, all the entities in the
  * `$data` array will appear, those that can be matched by primary key will get
  * the data merged, but those that cannot, will be discarded.
  *
  * You can limit fields that will be present in the merged entities by
  * passing the `fieldList` option, which is also accepted for associations:
  *
  * ```
  * $articles = $this->Articles->patchEntities($articles, $this->request->data(), [
  *  'fieldList' => ['title', 'body'],
  *  'associated' => ['Tags', 'Comments.Users' => ['fieldList' => 'username']]
  *  ]
  * );
  * ```
  *
  * You can use the `Model.beforeMarshal` event to modify request data
  * before it is converted into entities.
  */
 public function patchEntities($entities, array $data, array $options = [])
 {
     if (!isset($options['associated'])) {
         $options['associated'] = $this->_associations->keys();
     }
     $marshaller = $this->marshaller();
     return $marshaller->mergeMany($entities, $data, $options);
 }