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 entites by one to many association * * @return array|NULL */ protected function getManyToOneData() { $association = Metadata::getMetadata($this->entity)->associations[$this->name]; if ($this->em->associationsMap->isMapped($this->entity, $this->name, $this->associationKey)) { return $this->em->find($association->targetEntity, $this->em->associationsMap->find($this->entity, $this->name, $this->associationKey)); } $targetMetadata = Metadata::getMetadata($association->targetEntity); $modificator = $this->getModificator($association->targetEntity, $association->targetColumn); $data = $this->em->connection->select("*")->from($targetMetadata->tableName)->where("[{$association->targetColumn}] = {$modificator}", $this->associationKey)->execute(); $entity = $this->em->getIdentityMap($association->targetEntity)->map($data->fetch()); $this->em->associationsMap->map($this->entity, $this->name, $this->associationKey, $targetMetadata->getPrimaryKeyValue($entity)); return $entity; }
/** * 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)); } } } }
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>'; $benchMarkData = "mapping time: {$mappingTime} mapping memory: {$mappingMemory} " . "total time: " . number_format((microtime(TRUE) - Debug::$time) * 1000, 1, '.', ' ') . "ms " . "total memory: " . number_format(memory_get_peak_usage() / 1000, 1, '.', ' ') . "kB"; file_put_contents(__DIR__ . "/benchmark.log", date("r") . " # " . $benchMarkData . PHP_EOL, FILE_APPEND);