$user = new GenericDomainObject('User'); $user->setProperty('Username', 'admin'); $user->setProperty('Password', md5('admin')); $user->setProperty('Name', 'Admin'); $user->setProperty('Email', 'root@localhost'); $userId = $orm->saveObject($user); $user->setProperty('UserID', $userId); // --- english attributes of the guestbook ------------------------------------------------------ $titleEn = new GenericDomainObject('Attribute'); $titleEn->setProperty('Name', 'title'); $titleEn->setProperty('Value', 'My guestbook'); $titleEn->addRelatedObject('Attribute2Language', $langEn); $descriptionEn = new GenericDomainObject('Attribute'); $descriptionEn->setProperty('Name', 'description'); $descriptionEn->setProperty('Value', 'This is my first guestbook instance of the guestbook2009 module!'); $descriptionEn->addRelatedObject('Attribute2Language', $langEn); // --- german attributes of the guestbook ------------------------------------------------------- $titleDe = new GenericDomainObject('Attribute'); $titleDe->setProperty('Name', 'title'); $titleDe->setProperty('Value', 'Mein Gästebuch'); $titleDe->addRelatedObject('Attribute2Language', $langDe); $descriptionDe = new GenericDomainObject('Attribute'); $descriptionDe->setProperty('Name', 'description'); $descriptionDe->setProperty('Value', 'Dies ist die erste Instanz des neuen guestbook2009 Moduls!'); $descriptionDe->addRelatedObject('Attribute2Language', $langDe); // --- save guestbook with attributes ----------------------------------------------------------- $guestbook->addRelatedObject('Guestbook2LangDepValues', $titleEn); $guestbook->addRelatedObject('Guestbook2LangDepValues', $descriptionEn); $guestbook->addRelatedObject('Guestbook2LangDepValues', $titleDe); $guestbook->addRelatedObject('Guestbook2LangDepValues', $descriptionDe); $orm->saveObject($guestbook);
/** * Deletes the given entry from the database. * * @param Entry $domEntry The guestbook entry to delete. * * @author Christian Achatz * @version * Version 0.1, 21.05.2009<br /> */ public function deleteEntry(Entry $domEntry) { $entry = new GenericDomainObject('Entry'); $entry->setProperty('EntryID', $domEntry->getId()); // Delete the attributes of the entry, so that the object // itself can be deleted. If we don't do this, the ORM // will not be glad, because the entry still has child objects! $attributes = $this->orm->loadRelatedObjects($entry, 'Entry2LangDepValues'); foreach ($attributes as $attribute) { $this->orm->deleteObject($attribute); } // delete associated users, because we don't need them anymore. $users = $this->orm->loadRelatedObjects($entry, 'Editor2Entry'); foreach ($users as $user) { $this->orm->deleteObject($user); } // now delete entry object (associations are deleted automatically) $this->orm->deleteObject($entry); }
/** * Creates an domain object by name and properties. * * @param string $objectName Name of the object in mapping table. * @param array $properties Properties of the object. * * @return GenericORMapperDataObject The desired object or null. * * @author Christian Achatz * @version * Version 0.1, 11.05.2008<br /> * Version 0.2, 30.05.2008 (Now returns null, if no properties are available)<br /> * Version 0.3, 15.06.2008 (Now uses the constructor of GenericDomainObject to set the object name)<br /> * Version 0.4, 15.01.2011 (Added support for own domain objects and event handler)<br /> */ protected function mapResult2DomainObject($objectName, $properties) { if ($properties !== false) { // create service object if needed if (isset($this->domainObjectsTable[$objectName])) { $class = $this->domainObjectsTable[$objectName]['Class']; $object = new $class($objectName); } else { $object = new GenericDomainObject($objectName); } // set data component and object name $object->setDataComponent($this); // map properties into object foreach ($properties as $propertyName => $propertyValue) { // re-map empty values for null fields to PHP null values if (isset($this->mappingTable[$objectName][$propertyName]) && stripos($this->mappingTable[$objectName][$propertyName], self::$NULL_FIELD_IDENTIFIER) !== false && empty($propertyValue)) { $propertyValue = null; } $object->setProperty($propertyName, $propertyValue); } // call event handler $object->afterLoad(); } else { $object = null; } return $object; }