Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Installation for all entities #232

Open
koraykupe opened this issue Apr 12, 2017 · 1 comment
Open

First Installation for all entities #232

koraykupe opened this issue Apr 12, 2017 · 1 comment
Labels

Comments

@koraykupe
Copy link

Hi,

What is best practice to create all entities by not defining all of them one by one?

I use

$spot->mapper('Entity\Post')->migrate();

command for each entity. Is there a better way?

Thank you.

@willemwollebrants
Copy link
Contributor

willemwollebrants commented Apr 12, 2017

Fwiw: I use the following function:

/**
 * @param string $dir
 * @param string $namespace
 * @param \Spot\Locator $locator
 */
function migrateEntities(string $dir, string $namespace, \Spot\Locator $locator)
{
    $contents = array_filter(scandir($dir), function ($entry) {
        return !in_array($entry, ['.', '..']);
    });

    foreach ($contents as $entry) {
        if (is_file($dir . DIRECTORY_SEPARATOR . $entry)) {
            $classname = $namespace . substr($entry, 0, -4);
            //check if the class exists and is an Entity
            if (class_exists($classname) && is_subclass_of($classname, \Spot\Entity::class)) {
                $locator->mapper($classname)->migrate();
            }
        } else {
            //must be a subdirectory, so scan that too
            migrateEntities(
                $dir . DIRECTORY_SEPARATOR . $entry,
                $namespace . "{$entry}\\",
                $locator
            );
        }
    }
}

It has 3 parameters:
the directory where your entities are stored, the namespace (with trailing ) and the Spot\Locator object. It will scan through all files and directories and if it finds a valid entity it will run the migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants