FOF30\Model\DataModel\Relation\BelongsToMany::setDataFromCollection PHP Метод

setDataFromCollection() публичный Метод

Populates the internal $this->data collection from the contents of the provided collection. This is used by DataModel to push the eager loaded data into each item's relation.
public setDataFromCollection ( Collection &$data, mixed $keyMap = null ) : void
$data FOF30\Model\DataModel\Collection The relation data to push into this relation
$keyMap mixed Passes around the local to foreign key map
Результат 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);
                }
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * @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, ''));
 }