Cake\ORM\Association\BelongsToMany::replaceLinks PHP Method

    public function replaceLinks(EntityInterface $sourceEntity, array $targetEntities, array $options = [])
    {
        $bindingKey = (array) $this->bindingKey();
        $primaryValue = $sourceEntity->extract($bindingKey);
        if (count(array_filter($primaryValue, 'strlen')) !== count($bindingKey)) {
            $message = 'Could not find primary key value for source entity';
            throw new InvalidArgumentException($message);
        }
        return $this->junction()->connection()->transactional(function () use($sourceEntity, $targetEntities, $primaryValue, $options) {
            $foreignKey = array_map([$this->_junctionTable, 'aliasField'], (array) $this->foreignKey());
            $hasMany = $this->source()->association($this->_junctionTable->alias());
            $existing = $hasMany->find('all')->where(array_combine($foreignKey, $primaryValue));
            $associationConditions = $this->conditions();
            if ($associationConditions) {
                $existing->contain($this->target()->alias());
                $existing->andWhere($associationConditions);
            }
            $jointEntities = $this->_collectJointEntities($sourceEntity, $targetEntities);
            $inserts = $this->_diffLinks($existing, $jointEntities, $targetEntities, $options);
            if ($inserts && !$this->_saveTarget($sourceEntity, $inserts, $options)) {
                return false;
            }
            $property = $this->property();
            if (count($inserts)) {
                $inserted = array_combine(array_keys($inserts), (array) $sourceEntity->get($property));
                $targetEntities = $inserted + $targetEntities;
            }
            ksort($targetEntities);
            $sourceEntity->set($property, array_values($targetEntities));
            $sourceEntity->dirty($property, false);
            return true;
        });
    }

Usage Example

Beispiel #1
0
 /**
  * Tests that replaceLink requires the sourceEntity to have primaryKey values
  * for the source entity
  *
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Could not find primary key value for source entity
  * @return void
  */
 public function testReplaceWithMissingPrimaryKey()
 {
     $config = ['sourceTable' => $this->article, 'targetTable' => $this->tag, 'joinTable' => 'tags_articles'];
     $assoc = new BelongsToMany('Test', $config);
     $entity = new Entity(['foo' => 1], ['markNew' => false]);
     $tags = [new Entity(['id' => 2]), new Entity(['id' => 3])];
     $assoc->replaceLinks($entity, $tags);
 }