Skip to content

Limit collections #10

@zhil

Description

@zhil

In order to generate fixtures from live data in symfony2 I use trappar/AliceGeneratorBundle and code like

$fixturesContext = FixtureGenerationContext::create()->setMaximumRecursion(2);
$fixtureGenerator = $this->get('trappar_alice_generator.fixture_generator');
file_put_contents("data.yml",$fixtureGenerator->generateYaml($someEntity,$fixturesContext));

The problem is - when $someEntity have large collections - generator just hang up.

I didnt founded solution, so I have added simple wrapper

<?php
namespace AppBundle\Test;

use Doctrine\Common\Collections\Criteria;

class FixturesCollection
{
    public static function limitCollection($entity, $collectionName, $limit)
    {
        $refMatchTeams = new \ReflectionProperty($entity,$collectionName);

        $criteria = Criteria::create()->setMaxResults($limit);
        $refMatchTeams->setAccessible(true);
        $refMatchTeams->setValue($entity,$refMatchTeams->getValue($entity)->matching($criteria));
    }
}

And use it like

$fixturesContext = FixtureGenerationContext::create()->setMaximumRecursion(2);
$fixtureGenerator = $this->get('trappar_alice_generator.fixture_generator');
FixturesCollection::limitCollection($someEntity, "largeCollection1",5);
FixturesCollection::limitCollection($someEntity, "largeCollection2",1);
file_put_contents("data.yml",$fixtureGenerator->generateYaml($someEntity,$fixturesContext));

I have few questions

  1. Do trappar/AliceGenerator already have solution for this problem? addPersistedObjectConstraint do not works, because large collections are still loaded and everything is extremely slow.
  2. If library doesnt have solution - I would like to create a PR. Do you have any ideas/suggestions about where should I put this snippet in bundle? Class name ideas? It would be nice to add some kind of "maximumCollectionSize" to context and apply it to all collections in context - what do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions