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

Good practices using constructor with parameters or nested class #1764

Closed
ipesanz opened this issue Aug 25, 2017 · 2 comments
Closed

Good practices using constructor with parameters or nested class #1764

ipesanz opened this issue Aug 25, 2017 · 2 comments

Comments

@ipesanz
Copy link

ipesanz commented Aug 25, 2017

Something I want to create are nested classes depending of another one, which have a One to One relationship. The main idea is acting as a unique reference to the parent object and allow in these way to modify only the "nested class" if it's necessary in the future.
From my point of view, this give me the power to split big classes into smaller ones that are part of the bigger one.

In my case I need to define something close to:

class Member
{
    // related to a person, whith his own attributes

    /**
     * The certification category of this user.
     * Normally is only one.
     * 
     * @var Certification
     * @ORM\OneToOne(targetEntity="Certification", mappedBy="mainMember")
     **/
    private $certification;

    public function __construct()
    {      
        $this->certification = new Certification($this);   
        // Create Certification class with this Member as a parameter
    }

Certification will store strings as differents id's for each certification the user will have

class Certification
{
    // other attributes

    /**
     * The certification owner, because there is only one
     *
     * @var Member
     * @ORM\OneToOne(targetEntity="Member", inversedBy="certification")
     **/
    protected $mainMember;

    public function __construct($mainMember)
    {   
        $this->mainMember = $mainMember;        //We receive the id of the member
        $mainMember->setCertification($this);        //We tell the member to refer to this certification
    }

I read previous issues like #22 or #223 and something I would like and I don't know if it would be possible to accomplish without overriding methods of EasyAdminBundle is:

  • Be able to upload fixtures with this architecture
  • Be able to create Member and then create the Certification automaticatelly

Some troubles I had before are:

When creating a New Member - 500 Internal Error:  Entities passed to the
choice field must be managed. Maybe persist them in the entity manager?  [Error
Trace](https://gist.github.com/ipesanz/39b5591038f2b491c9e04dcb87b863a3)

When creating a New Certification - 500 Internal Error: Type error: Too few
arguments to function AppBundle\Entity\Certification::__construct(), 0 passed in
/home/isaac/work/easy-admin-demo/vendor/javiereguiluz/easyadmin-
bundle/Controller/AdminController.php on line 429 and exactly 1 expected [Error
Trace](https://gist.github.com/ipesanz/8ab2224cba2e2d21fb513a923b9f2158)

Questions:

  • Which is the best way to configure .yml files and DinamicallyParsedConstructors used in EasyAdmin to these cases.
  • If I have to override constructors or controllers for one specific class, which would be the manner?
  • How can I handle with the Doctrine Entity Manager inside a Class? (extend its type instead of being only a Class type?
  • Exists other better ways to accomplish this although the classes are not being created at beginning (remember: I need to have only one Certificates instance for each member and not to modify it... it would be perfect if they have the same id)
@javiereguiluz
Copy link
Collaborator

I don't know if it would be possible to accomplish this without overriding methods of EasyAdminBundle: be able to create Member and then create the Certification automatically.

No, that won't be possible. You'll need to override at least the createNewEntity() as explained here: https://symfony.com/doc/master/bundles/EasyAdminBundle/book/complex-dynamic-backends.html

@javiereguiluz
Copy link
Collaborator

Closing ... but feel free to create new issues about other specific issues related to this. Thanks!

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

No branches or pull requests

2 participants