schema-generator icon indicating copy to clipboard operation
schema-generator copied to clipboard

[enhancement] generate entity constructors with all non-nullable properties

Open Evertt opened this issue 8 years ago • 1 comments

If my schema.yml says this:

Product:
  parent: false
  properties:
    name: { nullable: false }

Then I would love it if my generated Product.php then contains a constructor function which requires the $name property. Like this:

// src/AppBundle/Entity/Product.php

class Product
{
    // ...

    public function __construct(string $name)
    {
        $this->setName($name);
        // using the setter that already exists
        // so we can utilize any validation that that method might have
    }

    // ...
}

That way you can guarantee when I or another developer wants to initialize a new Product, that it will be a valid entity right after initialization.

Evertt avatar May 08 '17 17:05 Evertt

I'm looking into a way to implement this with a PR.

I actually have 2 questions: -should i think about a global setting (all classes will be generated with constructor), a local one(only the ones with a specific parameter set), or both?

-id generation: possibly different policies will be involved : sometimes the id will be auto-generated (in this case, is there a reason about "isNullable" for an auto-generated id is true?), sometimes we want to initialize a Uuid inside the constructor (but not pass it inside the parameters), sometimes we want to pass the Uuid in the parameters, etc...so i'm not sure which policy to implement...ideas? Maybe an additional "constructorParameter" configuration inside the ID configuration...

lemorragia avatar Oct 12 '17 14:10 lemorragia