From 66b34314d67c695b899b704feffbe723453f1dbf Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 4 May 2015 12:32:33 +0200 Subject: Use isCrossRef for a many-to-many relation --- Aufgabe06/generated-classes/Base/Product.php | 296 +++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) (limited to 'Aufgabe06/generated-classes/Base/Product.php') diff --git a/Aufgabe06/generated-classes/Base/Product.php b/Aufgabe06/generated-classes/Base/Product.php index 870e1c4..6bcf48d 100644 --- a/Aufgabe06/generated-classes/Base/Product.php +++ b/Aufgabe06/generated-classes/Base/Product.php @@ -2,6 +2,8 @@ namespace Base; +use \Category as ChildCategory; +use \CategoryQuery as ChildCategoryQuery; use \Product as ChildProduct; use \ProductCategory as ChildProductCategory; use \ProductCategoryQuery as ChildProductCategoryQuery; @@ -105,6 +107,16 @@ abstract class Product implements ActiveRecordInterface protected $collProductCategories; protected $collProductCategoriesPartial; + /** + * @var ObjectCollection|ChildCategory[] Cross Collection to store aggregation of ChildCategory objects. + */ + protected $collCategories; + + /** + * @var bool + */ + protected $collCategoriesPartial; + /** * Flag to prevent endless save loop, if this object is referenced * by another object which falls in this transaction. @@ -113,6 +125,12 @@ abstract class Product implements ActiveRecordInterface */ protected $alreadyInSave = false; + /** + * An array of objects scheduled for deletion. + * @var ObjectCollection|ChildCategory[] + */ + protected $categoriesScheduledForDeletion = null; + /** * An array of objects scheduled for deletion. * @var ObjectCollection|ChildProductCategory[] @@ -640,6 +658,7 @@ abstract class Product implements ActiveRecordInterface $this->collProductCategories = null; + $this->collCategories = null; } // if (deep) } @@ -750,6 +769,35 @@ abstract class Product implements ActiveRecordInterface $this->resetModified(); } + if ($this->categoriesScheduledForDeletion !== null) { + if (!$this->categoriesScheduledForDeletion->isEmpty()) { + $pks = array(); + foreach ($this->categoriesScheduledForDeletion as $entry) { + $entryPk = []; + + $entryPk[0] = $this->getId(); + $entryPk[1] = $entry->getId(); + $pks[] = $entryPk; + } + + \ProductCategoryQuery::create() + ->filterByPrimaryKeys($pks) + ->delete($con); + + $this->categoriesScheduledForDeletion = null; + } + + } + + if ($this->collCategories) { + foreach ($this->collCategories as $category) { + if (!$category->isDeleted() && ($category->isNew() || $category->isModified())) { + $category->save($con); + } + } + } + + if ($this->productCategoriesScheduledForDeletion !== null) { if (!$this->productCategoriesScheduledForDeletion->isEmpty()) { \ProductCategoryQuery::create() @@ -1528,6 +1576,248 @@ abstract class Product implements ActiveRecordInterface return $this->getProductCategories($query, $con); } + /** + * Clears out the collCategories collection + * + * This does not modify the database; however, it will remove any associated objects, causing + * them to be refetched by subsequent calls to accessor method. + * + * @return void + * @see addCategories() + */ + public function clearCategories() + { + $this->collCategories = null; // important to set this to NULL since that means it is uninitialized + } + + /** + * Initializes the collCategories crossRef collection. + * + * By default this just sets the collCategories collection to an empty collection (like clearCategories()); + * however, you may wish to override this method in your stub class to provide setting appropriate + * to your application -- for example, setting the initial array to the values stored in database. + * + * @return void + */ + public function initCategories() + { + $this->collCategories = new ObjectCollection(); + $this->collCategoriesPartial = true; + + $this->collCategories->setModel('\Category'); + } + + /** + * Checks if the collCategories collection is loaded. + * + * @return bool + */ + public function isCategoriesLoaded() + { + return null !== $this->collCategories; + } + + /** + * Gets a collection of ChildCategory objects related by a many-to-many relationship + * to the current object by way of the product_category cross-reference table. + * + * If the $criteria is not null, it is used to always fetch the results from the database. + * Otherwise the results are fetched from the database the first time, then cached. + * Next time the same method is called without $criteria, the cached collection is returned. + * If this ChildProduct is new, it will return + * an empty collection or the current collection; the criteria is ignored on a new object. + * + * @param Criteria $criteria Optional query object to filter the query + * @param ConnectionInterface $con Optional connection object + * + * @return ObjectCollection|ChildCategory[] List of ChildCategory objects + */ + public function getCategories(Criteria $criteria = null, ConnectionInterface $con = null) + { + $partial = $this->collCategoriesPartial && !$this->isNew(); + if (null === $this->collCategories || null !== $criteria || $partial) { + if ($this->isNew()) { + // return empty collection + if (null === $this->collCategories) { + $this->initCategories(); + } + } else { + + $query = ChildCategoryQuery::create(null, $criteria) + ->filterByProduct($this); + $collCategories = $query->find($con); + if (null !== $criteria) { + return $collCategories; + } + + if ($partial && $this->collCategories) { + //make sure that already added objects gets added to the list of the database. + foreach ($this->collCategories as $obj) { + if (!$collCategories->contains($obj)) { + $collCategories[] = $obj; + } + } + } + + $this->collCategories = $collCategories; + $this->collCategoriesPartial = false; + } + } + + return $this->collCategories; + } + + /** + * Sets a collection of Category objects related by a many-to-many relationship + * to the current object by way of the product_category cross-reference table. + * It will also schedule objects for deletion based on a diff between old objects (aka persisted) + * and new objects from the given Propel collection. + * + * @param Collection $categories A Propel collection. + * @param ConnectionInterface $con Optional connection object + * @return $this|ChildProduct The current object (for fluent API support) + */ + public function setCategories(Collection $categories, ConnectionInterface $con = null) + { + $this->clearCategories(); + $currentCategories = $this->getCategories(); + + $categoriesScheduledForDeletion = $currentCategories->diff($categories); + + foreach ($categoriesScheduledForDeletion as $toDelete) { + $this->removeCategory($toDelete); + } + + foreach ($categories as $category) { + if (!$currentCategories->contains($category)) { + $this->doAddCategory($category); + } + } + + $this->collCategoriesPartial = false; + $this->collCategories = $categories; + + return $this; + } + + /** + * Gets the number of Category objects related by a many-to-many relationship + * to the current object by way of the product_category cross-reference table. + * + * @param Criteria $criteria Optional query object to filter the query + * @param boolean $distinct Set to true to force count distinct + * @param ConnectionInterface $con Optional connection object + * + * @return int the number of related Category objects + */ + public function countCategories(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) + { + $partial = $this->collCategoriesPartial && !$this->isNew(); + if (null === $this->collCategories || null !== $criteria || $partial) { + if ($this->isNew() && null === $this->collCategories) { + return 0; + } else { + + if ($partial && !$criteria) { + return count($this->getCategories()); + } + + $query = ChildCategoryQuery::create(null, $criteria); + if ($distinct) { + $query->distinct(); + } + + return $query + ->filterByProduct($this) + ->count($con); + } + } else { + return count($this->collCategories); + } + } + + /** + * Associate a ChildCategory to this object + * through the product_category cross reference table. + * + * @param ChildCategory $category + * @return ChildProduct The current object (for fluent API support) + */ + public function addCategory(ChildCategory $category) + { + if ($this->collCategories === null) { + $this->initCategories(); + } + + if (!$this->getCategories()->contains($category)) { + // only add it if the **same** object is not already associated + $this->collCategories->push($category); + $this->doAddCategory($category); + } + + return $this; + } + + /** + * + * @param ChildCategory $category + */ + protected function doAddCategory(ChildCategory $category) + { + $productCategory = new ChildProductCategory(); + + $productCategory->setCategory($category); + + $productCategory->setProduct($this); + + $this->addProductCategory($productCategory); + + // set the back reference to this object directly as using provided method either results + // in endless loop or in multiple relations + if (!$category->isProductsLoaded()) { + $category->initProducts(); + $category->getProducts()->push($this); + } elseif (!$category->getProducts()->contains($this)) { + $category->getProducts()->push($this); + } + + } + + /** + * Remove category of this object + * through the product_category cross reference table. + * + * @param ChildCategory $category + * @return ChildProduct The current object (for fluent API support) + */ + public function removeCategory(ChildCategory $category) + { + if ($this->getCategories()->contains($category)) { $productCategory = new ChildProductCategory(); + + $productCategory->setCategory($category); + if ($category->isProductsLoaded()) { + //remove the back reference if available + $category->getProducts()->removeObject($this); + } + + $productCategory->setProduct($this); + $this->removeProductCategory(clone $productCategory); + $productCategory->clear(); + + $this->collCategories->remove($this->collCategories->search($category)); + + if (null === $this->categoriesScheduledForDeletion) { + $this->categoriesScheduledForDeletion = clone $this->collCategories; + $this->categoriesScheduledForDeletion->clear(); + } + + $this->categoriesScheduledForDeletion->push($category); + } + + + return $this; + } + /** * Clears the current object, sets all attributes to their default values and removes * outgoing references as well as back-references (from other objects to this one. Results probably in a database @@ -1564,9 +1854,15 @@ abstract class Product implements ActiveRecordInterface $o->clearAllReferences($deep); } } + if ($this->collCategories) { + foreach ($this->collCategories as $o) { + $o->clearAllReferences($deep); + } + } } // if ($deep) $this->collProductCategories = null; + $this->collCategories = null; } /** -- cgit v1.2.3-70-g09d2