@param array $item
@param string $idField

@return int last insert id
@throws \WebComplete\microDb\Exception
12361
public function insert(array $item, string $idField = 'id'): int
    {
        $collectionData = $this->storage->load(true);
        if (!isset($collectionData['items'])) {
            $collectionData['items'] = [];
        }
        if (!isset($collectionData['inc'])) {
            $collectionData['inc'] = 1;
        }
        $item[$idField] = $collectionData['inc']++;
        $collectionData['items'][] = $item;
        $this->storage->save($collectionData);
        return $item[$idField];
    }
792370
public function saveItem(StorableInterface $item)
    {
        if ($this->tableExists() === false) {
            /** @todo Optionnally turn off for some models */
            $this->createTable();
        }

        if ($item !== null) {
            $this->setModel($item);
        }
        $model  = $this->model();
        $table  = $this->table();
        $struct = array_keys($this->tableStructure());
        $fields = $this->getModelFields($model);

        $keys   = [];
        $values = [];
        $binds  = [];
        $types  = [];
        foreach ($fields as $field) {
            $key = $field->ident();
            if (in_array($key, $struct)) {
                $keys[]      = '`'.$key.'`';
                $values[]    = ':'.$key.'';
                $binds[$key] = $field->val();
                $types[$key] = $field->sqlPdoType();
            }
        }

        $query = '
            INSERT
                INTO
            `'.$table.'`
                ('.implode(', ', $keys).')
            VALUES
                ('.implode(', ', $values).')';

        $result = $this->dbQuery($query, $binds, $types);

        if ($result === false) {
            throw new PDOException('Could not save item.');
        } else {
            if ($model->id()) {
                return $model->id();
            } else {
                return $this->db()->lastInsertId();
            }
        }
    }
670781
public function refillFromSearchResult(Item $item, $field, array $data)
    {
        if (!empty($data['url'])) {
            $item->addSource((new Source())->setUrl($data['url']));
            $item = $this->refill($item, $field);
        }

        return $item;
    }
6834
public function refill(Item $item, $field)
    {
        if (!($url = $this->getSourceForFill($item))) {
            return $item;
        }

        // get data
        preg_match(Filler::REG_ITEM_ID, $url, $match);
        $path = str_replace('#ID#', $match['id'], Filler::FILL_URL);
        $body = $this->browser->get($path);

        switch ($field) {
            case self::FIELD_DATE_END:
                if ($body['released_on']) {
                    $item->setDateEnd(new \DateTime($body['released_on']));
                }
                break;
            case self::FIELD_DATE_PREMIERE:
                $item->setDatePremiere(new \DateTime($body['aired_on']));
                break;
            case self::FIELD_DURATION:
                $item->setDuration($body['duration']);
                break;
            case self::FIELD_EPISODES_NUMBER:
                $ep_num = $body['episodes_aired'] ? $body['episodes_aired'] : $body['episodes'];
                $item->setEpisodesNumber($ep_num.($body['ongoing'] ? '+' : ''));
                break;
            case self::FIELD_GENRES:
                $new_item = $this->filler->setGenres(new Item(), $body);
                foreach ($new_item->getGenres() as $new_genre) {
                    $item->addGenre($new_genre);
                }
                break;
            case self::FIELD_IMAGES:
                $this->filler->setImages($item, $body);
                break;
            case self::FIELD_NAMES:
                $new_item = $this->filler->setNames(new Item(), $body);
                // set main name in top of names list
                $names = $new_item->getNames()->toArray();
                array_unshift($names, (new Name())->setName($new_item->getName()));
                foreach ($names as $new_name) {
                    $item->addName($new_name);
                }
                break;
            case self::FIELD_SOURCES:
                $new_item = $this->filler->setSources(new Item(), $body);
                foreach ($new_item->getSources() as $new_source) {
                    $item->addSource($new_source);
                }
                break;
            case self::FIELD_STUDIO:
                $this->filler->setStudio($item, $body);
                break;
            case self::FIELD_SUMMARY:
                $item->setSummary($body['description']);
                break;
        }

        return $item;
    }
924319
public function isCanRefill(Item $item, $field)
    {
        return in_array($field, $this->supported_fields) && $this->getSourceForFill($item);
    }
1125327
public function getId()
    {
        $idField = ItemInterface::IDFIELD;

        if (isset($this->_fields[$idField])) {
            return $this->_fields[$idField];
        }

        return NULL;
    }
27633
protected function addAttributes(\DOMElement $element, ItemFieldInterface $field, ItemInterface $item = null)
    {
        foreach ($field->getAttributes() as $key => $value) {
            if ($item) {
                $key = method_exists($item, $key) ? call_user_func([$item, $key]) : $key;
                $value = method_exists($item, $value) ? call_user_func([$item, $value]) : $value;
            }

            $element->setAttribute($key, $value);
        }
    }
1152100
public function add($item)
    {
        $result = false;
        if (isset($item['id'])) unset($item['id']);
        if (!isset($item['profile'])) $item['profile'] = array();
        $item['profile'] = encodeOptions($item['profile']);
        if (Eresus_CMS::getLegacyKernel()->db->insert($this->table, $item))
            $result = $this->get(Eresus_CMS::getLegacyKernel()->db->getInsertedId());
        return $result;
    }
774046
protected function insertAt($index, $item)
    {
        if ($this->getReadOnly()) {
            throw new Exception('只读list，不允许执行插入操作', 100100503);
        }

        if ($this->_count === $index) {
            $this->_data[$this->_count++] = $item;
        } elseif ($index >= 0 && $index < $this->_count) {
            array_splice($this->_data, $index, 0, [$item]);
            $this->_count++;
        } else {
            throw new Exception(str_cover('List索引"{index}"已超出范围', [
                '{index}' => $index,
            ]), 100100504);
        }
    }
1005765
public function getItemId(Item $item)
    {
        /* @var $source \AnimeDb\Bundle\CatalogBundle\Entity\Source */
        foreach ($item->getSources() as $source) {
            if (strpos($source->getUrl(), $this->browser->getHost()) === 0 &&
                preg_match(self::REG_ITEM_ID, $source->getUrl(), $match)
            ) {
                return (int)$match['id'];
            }
        }
        return 0;
    }
335072
protected function getRowId(array $row, $idField)
    {
        if (!array_key_exists($idField, $row)) {
            throw new ReaderException(
                sprintf(
                    'Row: "%s" has no field named "%s"',
                    $this->key(),
                    $idField
                )
            );
        }

        return $row[$idField];
    }
763511
protected function getRefillAttr($field, ItemEntity $item = null)
    {
        // item exists and can be refilled
        if ($item instanceof ItemEntity && $item->getName() &&
            ($plugins = $this->chain->getPluginsThatCanFillItem($item, $field))
        ) {
            /* @var $plugin RefillerInterface */
            foreach ($plugins as $key => $plugin) {
                $plugins[$key] = [
                    'name' => $plugin->getName(),
                    'title' => $plugin->getTitle(),
                    'can_refill' => $plugin->isCanRefill($item, $field), // can refill or can search
                ];
            }

            return [
                'data-type' => 'refill',
                'data-plugins' => $this->templating->render(
                    'AnimeDbCatalogBundle:Form:refillers.html.twig',
                    [
                        'item' => $item,
                        'field' => $field,
                        'plugins' => $plugins,
                    ]
                ),
            ];
        }

        return [];
    }
301088
protected function sync(DoplioDbTable $item)
    {
        if ($item->getKey() && !$item->exists) {
            $with = [];
            foreach ($item->getRelations() as $withName => $withData) {
                $with[] = $withName;
            }
            $item = $item->with($with)->find(
                $item->getKey()
            );
        }

        return $item;
    }
823635
public function flush(DoplioDbTable $item)
    {
        try {
            $this->table->getConnection()->transaction(
                function () use ($item) {
                    $this->flushItem($item);
                }
            );
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
220315
public function flushErrorByItem(Item $item) : int
    {
        $queryBuilder = $this->getQueryBuilder();
        $affectedRows = $this->getPreparedFlushErrorQuery($queryBuilder)
            ->andWhere(
                $queryBuilder->expr()->eq('uid', $item->getIndexQueueUid())
            )
            ->execute();
        return $affectedRows;
    }
692122
protected function _setFirstLast($i, $itemCount, array $item = [])
    {
        $item = array_merge(['last' => false, 'first' => false], $item);
        if ($i == 1) {
            $item['first'] = true;
        }

        if ($i == $itemCount) {
            $item['last'] = true;
        }

        return $item;
    }
455276
public function put(Item $item, Context\Put $context = null)
    {
        $table = $item->getTable();

        if (null !== $this->logger) {
            $this->log('Put on table '.$table);
        }

        if (empty($table)) {
            throw new \Riverline\DynamoDB\Exception\AttributesException('Item do not have table defined');
        }

        $attributes = array();
        foreach ($item as $name => $attribute) {
            /** @var $attribute \Riverline\DynamoDB\Attribute */
            if ("" !== $attribute->getValue()) {
                // Only not empty string
                $attributes[$name] = $attribute->getForDynamoDB();
            }
        }
        $parameters = array(
            'TableName' => $table,
            'Item'      => $attributes,
        );

        if (null !== $context) {
            $parameters += $context->getForDynamoDB();
        }

        if (null !== $this->logger) {
            $this->log('Put request paramaters : '.print_r($parameters, true), Logger::DEBUG);
        }

        $response = $this->connector->putItem($parameters);

        if (null !== $this->logger) {
            $this->log('Put request response : '.print_r($response, true), Logger::DEBUG);
        }

        // Update write counter
        $this->addConsumedWriteUnits($table, floatval($response['ConsumedCapacityUnits']));

        return $this->populateAttributes($response);
    }