RedUNIT\Base\Cross::testAggregationInSelfRefNM PHP Метод

testAggregationInSelfRefNM() публичный Метод

If you happen to have two beans of the same type you can still have a shared list but not with a sense of direction. I.e. quest->sharedQuest returns all the quests that follow the first one, but also the ones that are followed by the first one. If you want to have some sort of direction; i.e. one quest follows another one you'll have to use an alias: quest->target, but now you can't use the shared list anymore because it will start looking for a type named 'target' (which is just an alias) for quest, but it cant find that table and it's not possible to 'keep remembering' the alias throughout the system. The aggr() method solves this inconvenience. Aggr iterates through the list identified by its first argument ('target' -> ownQuestTargetList) and fetches every ID of the target (quest_target.target_id), loads these beans in batch for optimal performance, puts them back in the beans (questTarget->target) and returns the references.
public testAggregationInSelfRefNM ( ) : void
Результат void
    public function testAggregationInSelfRefNM()
    {
        R::nuke();
        $quest1 = R::dispense('quest');
        $quest1->name = 'Quest 1';
        $quest2 = R::dispense('quest');
        $quest2->name = 'Quest 2';
        $quest3 = R::dispense('quest');
        $quest3->name = 'Quest 3';
        $quest4 = R::dispense('quest');
        $quest4->name = 'Quest 4';
        $quest1->link('questTarget')->target = $quest2;
        $quest1->link('questTarget')->target = $quest3;
        $quest3->link('questTarget')->target = $quest4;
        $quest3->link('questTarget')->target = $quest1;
        R::storeAll(array($quest1, $quest3));
        //There should be 4 links
        asrt((int) R::count('questTarget'), 4);
        $quest1 = $quest1->fresh();
        $targets = $quest1->aggr('ownQuestTargetList', 'target', 'quest');
        //can we aggregate the targets over the link type?
        asrt(count($targets), 2);
        //are the all valid beans?
        foreach ($targets as $target) {
            //are they beans?
            asrt($target instanceof OODBBean, TRUE);
            //are they fetched as quest?
            asrt($target->getMeta('type'), 'quest');
        }
        //list target should already have been loaded, nuke has no effect
        R::nuke();
        $links = $quest1->ownQuestTargetList;
        //are the links still there, have they been set in the beans as well?
        asrt(count($links), 2);
        //are they references instead of copies, changes in the aggregation set should affect the beans in links!
        foreach ($targets as $target) {
            $target->name .= 'b';
            asrt(substr($target->name, -1), 'b');
        }
        //do the names end with a 'b' here as well ? i.e. have they been changed through references?
        foreach ($links as $link) {
            asrt(substr($target->name, -1), 'b');
        }
        //now test the effect on existing shadow...
        R::nuke();
        $quest1 = R::dispense('quest');
        $quest1->name = 'Quest 1';
        $quest2 = R::dispense('quest');
        $quest2->name = 'Quest 2';
        $quest3 = R::dispense('quest');
        $quest3->name = 'Quest 3';
        $quest4 = R::dispense('quest');
        $quest4->name = 'Quest 4';
        $quest1->link('questTarget')->target = $quest2;
        $quest1->link('questTarget')->target = $quest3;
        R::store($quest1);
        asrt((int) R::count('questTarget'), 2);
        //now lets first build a shadow
        $quest1->link('questTarget')->target = $quest4;
        //$quest1 = $quest1->fresh();
        $targets = $quest1->aggr('ownQuestTargetList', 'target', 'quest');
        //targets should not include the new bean...
        asrt(count($targets), 2);
        //this should not overwrite the existing beans
        R::store($quest1);
        asrt((int) R::count('questTarget'), 3);
    }