| @@ 84-105 (lines=22) @@ | ||
| 81 | * @param Record[]|Record $instances |
|
| 82 | * @return int |
|
| 83 | */ |
|
| 84 | public function associate($instances) |
|
| 85 | { |
|
| 86 | $this->verify($instances); |
|
| 87 | ||
| 88 | $this->dissociate(); |
|
| 89 | $this->attach($instances); |
|
| 90 | ||
| 91 | $ids = static::attributeList($instances, 'id'); |
|
| 92 | ||
| 93 | $successful = 0; |
|
| 94 | ||
| 95 | foreach ($this->related as $model) { |
|
| 96 | $this->persist($model); |
|
| 97 | ||
| 98 | if (!$ids || in_array($model->id(), $ids)) { |
|
| 99 | $model->set($this->foreignKey, $this->parent->id()); |
|
| 100 | $successful += $model->save(); |
|
| 101 | } |
|
| 102 | } |
|
| 103 | ||
| 104 | return (int) $successful; |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Dissociate the related model. |
|
| @@ 111-133 (lines=23) @@ | ||
| 108 | * @param Record[]|Record $instances |
|
| 109 | * @return int |
|
| 110 | */ |
|
| 111 | public function associate($instances) |
|
| 112 | { |
|
| 113 | $this->verify($instances); |
|
| 114 | ||
| 115 | $this->load(); |
|
| 116 | ||
| 117 | $this->attach($instances); |
|
| 118 | ||
| 119 | $ids = static::attributeList($instances, 'id'); |
|
| 120 | ||
| 121 | $successful = 0; |
|
| 122 | ||
| 123 | foreach ($this->related as $model) { |
|
| 124 | $this->persist($model); |
|
| 125 | ||
| 126 | if (!$ids || in_array($model->id(), $ids)) { |
|
| 127 | $model->set($this->foreignKey, $this->parent->id()); |
|
| 128 | $successful += $model->save(); |
|
| 129 | } |
|
| 130 | } |
|
| 131 | ||
| 132 | return (int) $successful; |
|
| 133 | } |
|
| 134 | ||
| 135 | /** |
|
| 136 | * Retrieve the related models. |
|