Example #1
0
 public function newContainer()
 {
     $container = new BaseContainer();
     $container->classSet($this->class);
     $container->add(func_get_args());
     $container->propertyMapper = $this->propertyMapper;
     return $container;
 }
Example #2
0
 public function testFindByContainer()
 {
     // make a container that contains objects for id
     $c = 0;
     while ($c++ < 10) {
         $o = new \stdclass();
         $o->c = $c;
         $objs[$c] = $o;
         $set[] = new EBTC($o, "name-{$c}");
     }
     $container = new Container($set);
     $oneTwoThree = new Container($objs[1], $objs[2], $objs[3]);
     $found = $container->findById($oneTwoThree)->setPropertyMapper();
     $this->assertSame($found->count(), 3);
     $this->assertSame($found->pluck('id')->setPropertyMapper()->pluck('c'), array(1, 2, 3));
 }
Example #3
0
 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);
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 public function testFindBySetUnersistedViaMultiton()
 {
     $repo = $this->entityManager->getRepository('A1');
     $a1s = array();
     foreach (range(1, 100) as $n) {
         $a1 = $repo->make();
         $a1->setDirect(array('id' => $n));
         $a1s[$n] = $a1;
     }
     $keys = array(5, 9, 15, 56, 99);
     $container = new Container(array_intersect_key($a1s, array_flip($keys)));
     $this->assertTrue($container->isSameAs($repo->findBySet(new Integer($keys))));
 }
Example #6
0
 /**
  * Are the contents of a container identical to another container.
  * This a === b if a contains b and count(a) = count(b);
  * @param Container $container
  * @param bool Is ordering significant. Is the order of elements in a container significant.
  * @return bool
  */
 public function isSameAs(Container $container, $orderingIsSignificant = false)
 {
     $comparingClass = $container->classGet();
     $thisClass = $this->classGet();
     if ($comparingClass and $thisClass and $thisClass !== $comparingClass) {
         return false;
     }
     if ($orderingIsSignificant) {
         return array_keys($this->collection) === array_keys($container->collection);
     } else {
         return $this->contains($container) and count($container) === count($this);
     }
 }
Example #7
0
 /**
  * Get a new Container
  * @param string Class
  * @return Bond\Container
  */
 protected function makeNewContainer()
 {
     $container = new Container();
     $container->classSet($this->entityClass);
     $container->setPropertyMapper(PropertyMapperEntityData::class);
     return $container;
 }