Symfony2 Doctrine2 trouble with optional one to one relation

后端 未结 5 1530
小蘑菇
小蘑菇 2021-01-01 12:53

I have a problem with Doctrine2 in Symfony2 and two relationed entities.

There is a user-entity that can (not must) have a usermeta-entity referenced which

相关标签:
5条回答
  • 2021-01-01 13:00

    I hope I do not disturb anyone by submitting this very late answer, but here is how I solved this problem:

    /**
     * @var Takeabyte\GripBundle\Entity\PDF
     * @ORM\OneToOne(targetEntity="Takeabyte\GripBundle\Entity\PDF", inversedBy="element", fetch="EAGER", orphanRemoval=true)
     */
    protected $pdf = null;
    

    I added = null; to the attribute declaration. I hope this is of any help for anyone who reads this.

    0 讨论(0)
  • 2021-01-01 13:09

    You're using the wrong type of Relation for your problem.

    What you want is a unidirectional one to one from Usermeta to User.

    A bidirectional one to one relationship would mean the following:

    1. A user MUST have a Usermeta object.
    2. A Usermeta object MUST have a User.

    In your case you're only trying to require the second condition.

    This does mean that you can only hydrate User from Usermeta and not the other way around.

    Unfortunately doctrine does not support Zero or One to Many relationships.

    0 讨论(0)
  • 2021-01-01 13:16

    I got the error message "spl_object_hash() expects parameter 1 to be object, null given in..." while trying the same thing. I tried to define a bidirectional One to One relationship while the inversed value could be null. This gave the error message. Taking away the inversed side of the relationship solved the problem. It is a pity that Zero or One to One relationships aren't supported.

    0 讨论(0)
  • 2021-01-01 13:20

    Reading my own old question is quite fun since I see the problem at first glance now..

    When it came to a solution I've thought that doctrine can only handle Ids named "id", but ... aduserid is just not marked as ID, it's missing the Id annotation and doctrine cannot use the fields for the join column..

    Second thing, Zdenek Machek was right: It has to be marked as nullable.

    0 讨论(0)
  • 2021-01-01 13:23

    The Zdenek Machek comment is almost correct. As you can see from the Doctrine2 documentation, the nullable option should be in the join annotation (@JoinColumn), not in the mapping one (@OneToOne).

    @JoinColumn doc:

    This annotation is used in the context of relations in @ManyToOne, @OneToOne fields and in the Context of @JoinTable nested inside a @ManyToMany. This annotation is not required. If its not specified the attributes name and referencedColumnName are inferred from the table and primary key names.

    Required attributes:

    name: Column name that holds the foreign key identifier for this relation. In the context of @JoinTable it specifies the column name in the join table.

    referencedColumnName: Name of the primary key identifier that is used for joining of this relation.

    Optional attributes:

    unique: Determines if this relation exclusive between the affected entities and should be enforced so on the database constraint level. Defaults to false.

    nullable: Determine if the related entity is required, or if null is an allowed state for the relation. Defaults to true.

    onDelete: Cascade Action (Database-level)

    onUpdate: Cascade Action (Database-level)

    columnDefinition: DDL SQL snippet that starts after the column name and specifies the complete (non-portable!) column definition. This attribute allows to make use of advanced RMDBS features. Using this attribute on @JoinColumn is necessary if you need slightly different column definitions for joining columns, for example regarding NULL/NOT NULL defaults. However by default a “columnDefinition” attribute on @Column also sets the related @JoinColumn’s columnDefinition. This is necessary to make foreign keys work.

    http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#annref-joincolumn

    @OneToOne doc:

    The @OneToOne annotation works almost exactly as the @ManyToOne with one additional option that can be specified. The configuration defaults for @JoinColumn using the target entity table and primary key column names apply here too.

    Required attributes:

    targetEntity: FQCN of the referenced target entity. Can be the unqualified class name if both classes are in the same namespace. IMPORTANT: No leading backslash!

    Optional attributes:

    cascade: Cascade Option

    fetch: One of LAZY or EAGER

    orphanRemoval: Boolean that specifies if orphans, inverse OneToOne entities that are not connected to any owning instance, should be removed by Doctrine. Defaults to false.

    inversedBy: The inversedBy attribute designates the field in the entity that is the inverse side of the relationship.

    http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#onetoone

    0 讨论(0)
提交回复
热议问题