public function save($entity)
    {
        $query = $this->db->table($entity->definition()->getName());
        $primary = $entity->id();
        if (!isset($this->objects[$entity->definition()->getName()][base64_encode(serialize($primary))])) {
            $new = $query->insert($entity->toArray());
            $entity->fromArray($new);
            $this->objects[$entity->definition()->getName()][base64_encode(serialize($new))] = $entity;
        } else {
            foreach ($primary as $k => $v) {
                $query->filter($k, $v);
            }
            $query->update($entity->toArray());
            $new = [];
            foreach ($primary as $k => $v) {
                $new[$k] = $entity->{$k};
            }
            if (base64_encode(serialize($new)) !== base64_encode(serialize($primary))) {
                unset($this->objects[$entity->definition()->getName()][base64_encode(serialize($primary))]);
                $this->objects[$entity->definition()->getName()][base64_encode(serialize($new))] = $entity;
            }
        }
        return $this->lazy($entity, $entity->toArray());
    }