public function testManyToManyData2() { $tag = $this->manager->find('App\\Models\\Tag', 4); $this->assertTrue(in_array(2, array_keys($tag->applications))); $tag = $this->manager->find('App\\Models\\Tag', 4); $this->assertTrue(in_array(2, array_keys($tag->applications))); }
/** * Get entities by many to many association * * @return array|NULL */ protected function getManyToManyData() { $association = Metadata::getMetadata($this->entity)->associations[$this->name]; $targetMetadata = Metadata::getMetadata($association->targetEntity); $data = $this->em->connection->select("[{$targetMetadata->tableName}].*")->from($targetMetadata->tableName)->innerJoin($association->joinTable)->on("[{$association->joinTable}].[{$association->joinSourceColumn}] = " . $this->getModificator($this->entity, $association->sourceColumn), $this->associationKey)->and("[{$association->joinTable}].[{$association->joinTargetColumn}] = [" . $targetMetadata->tableName . "].[{$association->targetColumn}]")->execute(); $entities = $this->em->getIdentityMap($association->targetEntity)->map($data->fetchAssoc($targetMetadata->primaryKey)); if (!empty($entities)) { $this->em->associationsMap->map($this->entity, $this->name, $this->associationKey, array_keys($entities)); } return $entities; }
/** * Save associations * * @param mixed $entity */ protected function persistAssociations(&$entity) { $metadata = Metadata::getMetadata(get_class($entity)); $data = $metadata->getAssociationsValues($entity); foreach (array_merge($metadata->oneToOne, $metadata->oneToMany, $metadata->manyToMany) as $association) { if (!array_key_exists($association->name, $data)) { continue; } if ($association instanceof Associations\OneToOne) { if (empty($association->mapped)) { continue; } $original = $this->em->associationsMap->find(get_class($entity), $association->name, $metadata->getPrimaryKeyValue($entity)); if (empty($data[$association->name]) && !empty($original)) { $targetMetadata = Metadata::getMetadata($association->targetEntity); $this->em->getPersister($association->targetEntity)->persistInversedOneToOneAssociation($association, $original, NULL); } elseif (!empty($data[$association->name]) && empty($original)) { $this->em->getPersister($association->targetEntity)->persistInversedOneToOneAssociation($association, $data[$association->name], $metadata->getPrimaryKeyValue($entity)); } elseif (!empty($original) && !empty($data[$association->name]) && $this->em->find($association->targetEntity, $original) != $data[$association->name]) { $persister = $this->em->getPersister($association->targetEntity); $persister->persistInversedOneToOneAssociation($association, $original, NULL); $persister->persistInversedOneToOneAssociation($association, $data[$association->name], $metadata->getPrimaryKeyValue($entity)); } } if ($association instanceof Associations\OneToMany) { $original = $this->em->associationsMap->find(get_class($entity), $association->name, $metadata->getPrimaryKeyValue($entity)); if (empty($data[$association->name]) && !empty($original)) { $this->em->getPersister($association->targetEntity)->persistOneToManyAssociation($association, $original, NULL); } elseif (!empty($data[$association->name]) && empty($original)) { $this->em->getPersister($association->targetEntity)->persistOneToManyAssociation($association, $data[$association->name], $metadata->getPrimaryKeyValue($entity)); } elseif (!empty($original) && !empty($data[$association->name]) && $this->em->find($association->targetEntity, $original) != $data[$association->name]) { $persister = $this->em->getPersister($association->targetEntity); $persister->persistOneToManyAssociation($association, array_diff($original, $data[$association->name]), NULL); $persister->persistOneToManyAssociation($association, array_diff($data[$association->name], $original), $metadata->getPrimaryKeyValue($entity)); } } if ($association instanceof Associations\ManyToMany) { $original = $this->em->associationsMap->find(get_class($entity), $association->name, $metadata->getPrimaryKeyValue($entity)); if (empty($data[$association->name]) && !empty($original)) { $this->em->getPersister($association->sourceEntity)->persistManyToManyAssociation($association, $metadata->getPrimaryKeyValue($entity), NULL); } elseif (!empty($data[$association->name]) && empty($original)) { $this->em->getPersister($association->sourceEntity)->persistManyToManyAssociation($association, $metadata->getPrimaryKeyValue($entity), $data[$association->name]); } elseif (!empty($original) && !empty($data[$association->name]) && $this->em->find($association->sourceEntity, $original) != $data[$association->name]) { $persister = $this->em->getPersister($association->sourceEntity); $persister->persistManyToManyAssociation($association, $metadata->getPrimaryKeyValue($entity), array_diff($original, $data[$association->name]), TRUE); $persister->persistManyToManyAssociation($association, $metadata->getPrimaryKeyValue($entity), array_diff($data[$association->name], $original)); } } } }
/** * Get values * * @param mixed $entity * @return array */ protected function getValues(&$entity) { return $this->em->getIdentityMap($this->entity)->getSavedValues($entity); }
<?php namespace App; require_once __DIR__ . "/bootstrap.php"; use Nette\Debug, Nette\Framework, dibi; Debug::timer('benchmark'); $memory = memory_get_peak_usage(); echoBeginHtml(); /********************************************************************************************************************************/ // Setum entity manager $em = new \ActiveMapper\Manager(\dibi::getConnection()); echo "<h1>All authors</h1>"; // Get all authors $authors = $em->findAll('App\\Models\\Author'); foreach ($authors as $author) { Debug::dump($author->name); Debug::dump($author->blog->name); } echo "<h1>Author by ID #3</h1>"; // Get author by id $author = $em->find('App\\Models\\Author', 3); Debug::dump($author->name); Debug::dump($author->blog->name); /********************************************************************************************************************************/ // Benchmark data Debug::barDump(Framework::NAME . " " . Framework::VERSION . " " . Framework::REVISION); Debug::barDump("dibi " . dibi::VERSION . " " . dibi::REVISION); Debug::barDump($mappingTime = number_format(Debug::timer('benchmark') * 1000, 1, '.', ' ') . "ms", "Mapping Time"); Debug::barDump($mappingMemory = number_format((memory_get_peak_usage() - $memory) / 1000, 1, '.', ' ') . "kB", "Mapping Memory"); echo '<p><a href="http://github.com/Vrtak-CZ/ActiveMapper/blob/master/examples/index.php" target="_blank">' . 'Show code on GitHub</a> - <a href="http://am.vrtak-cz.net/coverage">Show coverage</a></p>';