private function set($field, $args)
    {
        $this->initializeDoctrine();

        $property = $this->cm->getProperty($field);

        if (! $property) {
            throw new BadMethodCallException("no field with name '" . $field . "' exists on '" . $this->cm->getClassName() . "'");
        }

        switch (true) {
            case $property instanceof FieldMetadata && ! $property->isPrimaryKey():
                $this->{$field} = $args[0];
                break;

            case $property instanceof ToOneAssociationMetadata:
                $targetClassName = $property->getTargetEntity();

                if ($args[0] !== null && ! ($args[0] instanceof $targetClassName)) {
                    throw new InvalidArgumentException("Expected persistent object of type '" . $targetClassName . "'");
                }

                $this->{$field} = $args[0];
                $this->completeOwningSide($property, $args[0]);
                break;
        }

        return $this;
    }