src/orm/classes/Field/Bindings.php
public function afterEntitySave(ORM_Entity $entity)
    {
        $inMemoryBindings = array();
        foreach ($entity->{$this->getName()} as $bindedEntity)
        {
            /** @var ORM_Entity $bindedEntity */
            $inMemoryBindings[$bindedEntity->getPrimaryKey()] = $bindedEntity;
        }

        $table = $this->getBindingsTable();
        $bindingsTableName = $table->getName();
        $sourceField = $table->getSourceField();

        /*
         * Загружаем список привязок, сохранённых в БД
         */
        $q = Eresus_DB::getHandler()->createSelectQuery();
        $q->select('*');
        $q->from($bindingsTableName);
        $q->where($q->expr->eq($sourceField, $q->bindValue($entity->getPrimaryKey())));
        $storedBindings = $q->fetchAll();

        /*
         * Удаляем из $inMemoryBindings те, что уже есть в БД. Добавляем в $toDelete те,
         * которые есть в БД, но которых нет в $inMemoryBindings.
         */
        $toDelete = array();
        foreach ($storedBindings as $storedBinding)
        {
            if (array_key_exists($storedBinding[$sourceField], $inMemoryBindings))
            {
                unset($inMemoryBindings[$storedBinding[$sourceField]]);
            }
            else
            {
                $toDelete []= $storedBinding;
            }
        }

        /*
         * Удаляем из БД привязки, которых нет у объекта
         */
        if (count($toDelete) > 0)
        {
            $q = Eresus_DB::getHandler()->createDeleteQuery();
            $keys = array();
            foreach ($toDelete as $record)
            {
                $keys []= $q->expr->lAnd(
                    $q->expr->eq($sourceField,
                        $q->bindValue($record[$sourceField], null, PDO::PARAM_INT)),
                    $q->expr->eq($this->getName(),
                        $q->bindValue($record[$this->getName()], null, PDO::PARAM_INT))
                );
            }
            $q->deleteFrom($bindingsTableName)->where($q->expr->lOr($keys));
            $q->execute();
        }

        /*
         * Сохраняем в БД те привязки, которых там ещё нет
         */
        if (count($inMemoryBindings) > 0)
        {
            $q = Eresus_DB::getHandler()->createInsertQuery();
            $q->insertInto($bindingsTableName);
            $q->set($sourceField, $q->bindValue($entity->getPrimaryKey()));
            $bindedId = null;
            $q->set($this->getName(), $q->bindParam($bindedId));
            foreach ($inMemoryBindings as $binding)
            {
                /** @var ORM_Entity $binding */
                /** @noinspection PhpUnusedLocalVariableInspection */
                $bindedId = $binding->getPrimaryKey();
                $q->execute();
            }
        }
    }
public function afterEntitySave(ORM_Entity $entity)
    {
        $inMemoryBindings = array();
        foreach ($entity->{$this->getName()} as $bindedEntity)
        {
            /** @var ORM_Entity $bindedEntity */
            $inMemoryBindings[$bindedEntity->getPrimaryKey()] = $bindedEntity;
        }

        $table = $this->getBindingsTable();
        $bindingsTableName = $table->getName();
        $sourceField = $table->getSourceField();

        /*
         * Загружаем список привязок, сохранённых в БД
         */
        $q = Eresus_DB::getHandler()->createSelectQuery();
        $q->select('*');
        $q->from($bindingsTableName);
        $q->where($q->expr->eq($sourceField, $q->bindValue($entity->getPrimaryKey())));
        $storedBindings = $q->fetchAll();

        /*
         * Удаляем из $inMemoryBindings те, что уже есть в БД. Добавляем в $toDelete те,
         * которые есть в БД, но которых нет в $inMemoryBindings.
         */
        $toDelete = array();
        foreach ($storedBindings as $storedBinding)
        {
            if (array_key_exists($storedBinding[$sourceField], $inMemoryBindings))
            {
                unset($inMemoryBindings[$storedBinding[$sourceField]]);
            }
            else
            {
                $toDelete []= $storedBinding;
            }
        }

        /*
         * Удаляем из БД привязки, которых нет у объекта
         */
        if (count($toDelete) > 0)
        {
            $q = Eresus_DB::getHandler()->createDeleteQuery();
            $keys = array();
            foreach ($toDelete as $record)
            {
                $keys []= $q->expr->lAnd(
                    $q->expr->eq($sourceField,
                        $q->bindValue($record[$sourceField], null, PDO::PARAM_INT)),
                    $q->expr->eq($this->getName(),
                        $q->bindValue($record[$this->getName()], null, PDO::PARAM_INT))
                );
            }
            $q->deleteFrom($bindingsTableName)->where($q->expr->lOr($keys));
            $q->execute();
        }

        /*
         * Сохраняем в БД те привязки, которых там ещё нет
         */
        if (count($inMemoryBindings) > 0)
        {
            $q = Eresus_DB::getHandler()->createInsertQuery();
            $q->insertInto($bindingsTableName);
            $q->set($sourceField, $q->bindValue($entity->getPrimaryKey()));
            $bindedId = null;
            $q->set($this->getName(), $q->bindParam($bindedId));
            foreach ($inMemoryBindings as $binding)
            {
                /** @var ORM_Entity $binding */
                /** @noinspection PhpUnusedLocalVariableInspection */
                $bindedId = $binding->getPrimaryKey();
                $q->execute();
            }
        }
    }
Действия, выполняемые после сохранения сущности

@param ORM_Entity $entity