public function newContainer() { $container = new BaseContainer(); $container->classSet($this->class); $container->add(func_get_args()); $container->propertyMapper = $this->propertyMapper; return $container; }
public function testInsert2() { $a1r = $this->entityManager->getRepository('A1'); $a2r = $this->entityManager->getRepository('A2'); $string = "some value \\ \\' \" "; $container = new Container(); foreach (range(1, 10) as $n) { $a1 = $a1r->make(); $a1->set('string', $string); $a1->set('foreign_key', $a2r->make()); $container->add($a1); } $a1Task = new Persist($this->entityManager->recordManager); $a1Task->setObject($container); $numQuerys = $this->db->numQuerys; $this->assertTrue($a1Task->execute($this->db)); // I'm not sure we can continue with this tests below because chainsaving, additional tasks, ... // effect this in ways that, while determinsitic, probably shouldn't be tested in this manner return; $this->assertSame($numQuerys + 2, $this->db->numQuerys); $this->assertSame($this->db->query(new Query("SELECT count(*) FROM A1"))->fetch(Result::FETCH_SINGLE), (string) count($container)); $this->assertSame($this->db->query(new Query("SELECT count(*) FROM A2"))->fetch(Result::FETCH_SINGLE), (string) count($container)); $this->assertSame($this->db->query(new Query("SELECT DISTINCT string FROM A1"))->fetch(Result::FETCH_SINGLE), $string); }
/** * Get array of links to this table * @return array */ public function getLinks() { if (isset($this->links)) { return $this->links; } // build links $this->links = array(); foreach ($this->getAttributes() as $column) { /* This relation has a link if it has a attribute which has all of the following * 1. Is unique and is referenced by another attribute TODO. Pete: We don't currently support dual column foreign key constraints * 2. This another attribute is one half of a dualcolumn primary key * TODO. Pete: I think this can be relaxed a little. We just need a unique constraint which references two tables * 3. The other half of the primary references another unique constraint in another table */ // See 1. if ($column->isUnique() and $references = $column->getIsReferencedBy()) { foreach ($references as $reference) { // See 2. if ($reference->isPrimaryKey() and count($primaryKeys = $reference->getRelation()->getPrimaryKeys()) == 2) { $primaryKeys->remove($reference); $otherHalfofPrimaryKey = $primaryKeys->pop(); // See 3. $otherReferences = $otherHalfofPrimaryKey->getReferences(); $foreignEntities = new BaseContainer(); foreach ($otherReferences as $otherReference) { // Pete. We might not need the unique check here because foreign keys already have uniqueness enforced if ($otherReference->isUnique() and $otherReference->getRelation() !== $this) { $foreignEntities[] = $foreignEntities->add($otherReference->getRelation()); $foreignEntityReference = $otherReference; } } // have we a link? if ($foreignEntities) { $sourceEntity = $this->getEntityName(); $linkEntity = $otherHalfofPrimaryKey->getRelation()->getEntityName(); $refSource = array($column->name, $reference->name); $refForeign = array($otherHalfofPrimaryKey->name, $foreignEntityReference->name); // get columns and look for a ranking columns $possibleRankingAttributes = $otherHalfofPrimaryKey->getRelation()->getAttributes(); $possibleRankingAttributes->remove($otherHalfofPrimaryKey, $reference); $sortColumn = null; $rankingRegex = "/^{$otherHalfofPrimaryKey->name}_?(r|rank|ranking)\$/i"; foreach ($possibleRankingAttributes as $ranking) { if ($ranking->getType()->isNumeric() and preg_match($rankingRegex, $ranking->name)) { $sortColumn = $ranking->name; break; } } // sort foreignEntities, we want 'root' entity first then alphabetical $foreignEntities = $foreignEntities->sort(function ($a, $b) { if ($a->isInherited() and !$b->isInherited()) { return 1; } return $a->getEntityName() < $b->getEntityName() ? -1 : 1; })->map(function ($e) { return $e->getEntityName(); }); $this->links[$linkEntity] = new Link($sourceEntity, $linkEntity, array_values($foreignEntities), $refSource, $refForeign, $sortColumn); } } } } } return $this->links; }
public function testFindAllPersistedUnpersistedChanged() { $repo = $this->entityManager->getRepository(new A1()); // generate and persist a load of A1s $a1s_saved = $repo->findAll(); // generate and __DONT__ persist a load of A1s $a1s_new = new Container(); foreach ($a1s_saved as $_) { $a1s_new->add($repo->make()); } $a1s_unpersisted = $repo->findUnpersisted(); $a1s_persisted = $repo->findPersisted(); $a1s_changed = $repo->findChanged(); $a1s_all = $repo->findAll(); $this->assertTrue($a1s_saved->isSameAs($a1s_persisted)); $this->assertTrue($a1s_new->isSameAs($a1s_unpersisted)); $this->assertTrue($a1s_new->isSameAs($a1s_changed)); $this->assertTrue($a1s_all->contains($a1s_persisted, $a1s_unpersisted)); $this->assertSame(count($a1s_all), count($a1s_saved) + count($a1s_new)); }