public setDataFromCollection ( |
||
$data | The relation data to push into this relation | |
$keyMap | mixed | Passes around the local to foreign key map |
Résultat | void |
public function setDataFromCollection(DataModel\Collection &$data, $keyMap = null)
{
$this->data = new DataModel\Collection();
if (!is_array($keyMap)) {
return;
}
if (!empty($data)) {
// Get the local key value
$localKeyValue = $this->parentModel->getFieldValue($this->localKey);
// Make sure this local key exists in the (cached) pivot table
if (!isset($keyMap[$localKeyValue])) {
return;
}
/** @var DataModel $item */
foreach ($data as $key => $item) {
// Only accept foreign items whose key is associated in the pivot table with our local key
if (in_array($item->getFieldValue($this->foreignKey), $keyMap[$localKeyValue])) {
$this->data->add($item);
}
}
}
}
/** * @group BelongsToMany * @group BelongsToManySetDataFromCollection * @covers FOF30\Model\DataModel\Relation\BelongsToMany::setDataFromCollection * @dataProvider BelongsToManyDataprovider::getTestSetDataFromCollection */ public function testSetDataFromCollection($test, $check) { $msg = 'BelongsToMany::setDataFromCollection %s - Case: ' . $check['case']; $parts = new Parts(static::$container); $model = new Groups(static::$container); $model->find(2); $relation = new BelongsToMany($model, 'Parts'); $items[0] = clone $parts; $items[0]->find(1); $items[1] = clone $parts; $items[1]->find(2); $items[2] = clone $parts; $items[2]->find(3); $data = new Collection($items); $relation->setDataFromCollection($data, $test['keymap']); $this->assertCount($check['count'], ReflectionHelper::getValue($relation, 'data'), sprintf($msg, '')); }