function save() { Assert::isFalse($this->isReadonly(), 'cannot save readonly collections'); $getter = $this->referentialProperty->getGetter(); $setter = $this->referentialProperty->getSetter(); if ($this->referentialProperty->getMultiplicity()->isNullable()) { foreach ($this->getLostTracked() as $object) { $object->{$setter}(null); $object->save(); } } else { if (sizeof($this->getLostTracked())) { $query = new EntityQuery($this->getChildren()); $this->fillQuery($query); $query->andWhere(Expression::in($this->getChildren()->getLogicalSchema()->getIdentifier(), $this->getLostTracked())); $query->delete(); } } foreach ($this->getList() as $object) { // avoid useless mutation if ($object->{$getter}() !== $this->getParentObject()) { $object->{$setter}($this->getParentObject()); } $object->save(); } }
/** * Create a php code which implements a setter within the entity class * @param IMappable $entity * @param OrmProperty $property */ function toSetter(IMappable $entity, OrmProperty $property) { $argCastType = ($argCastType = $this->getImplClass()) ? $argCastType . ' ' : ''; $argDocType = $argCastType ? $this->getImplClass() : 'scalar'; $defaultValue = $property->getMultiplicity()->isNullable() ? ' = null' : ''; $propertyName = $property->getName(); $capitalizedPropertyName = ucfirst($propertyName); return <<<EOT \t/** \t * @param {$argDocType} \${$propertyName} \t * @return {$entity->getLogicalSchema()->getEntityName()} itself \t */ \tfunction {$property->getSetter()}({$argCastType}\${$propertyName}{$defaultValue}) \t{ \t\t\$this->{$propertyName} = \${$propertyName}; \t\treturn \$this; \t} EOT; }