Coder Social home page Coder Social logo

Comments (8)

patrickbrouwers avatar patrickbrouwers commented on July 2, 2024

Hey @yuloh

hasOne mirrors what hasOne does in Eloquent. (https://laravel.com/docs/5.6/eloquent-relationships#one-to-one) It's correct that it returns a oneToOne.

belongsTo correctly returns a ManyToOne (just like Eloquent).

You don't have to use hasOne and belongsTo if the terms are confusing. They were only added to make the transition from Eloquent easier. Using oneToOne or manyToOne directly is perfectly fine.

from fluent.

matt-allan avatar matt-allan commented on July 2, 2024

Hi @patrickbrouwers,

My issue isn't that the terms are confusing; it's that they cause schema validation to fail. If you check out the branch I created and run the tests you can look at the error being reported by doctrine.

Specifically it seems that doctrine thinks that a many-to-one can only be mapped to a one-to-many. Because hasOne is a one-to-one relationship and belongsTo is a many-to-one it causes an error.

The code seems to run fine but it reports an error when using the doctrine:schema:validate command.

from fluent.

patrickbrouwers avatar patrickbrouwers commented on July 2, 2024

You should build your mapping just as you would do with any other driver. Fluent is just an alternative syntax to Doctrine's mapping api.
In the end it's the user's choice if he uses a one to one or a many to one and make sure the schema is valid. There's nothing Fluent can and should do about that.

Looking at your example:

If association App\Domain\User\Settings#user is many-to-one, then the inversed side App\Domain\User#settings has to be one-to-many

You should have

$user->hasMany(Setting::class)->... (or oneToMany)

and

$setting->belongsTo(User::class)->... (or manyToOne)

It's not Fluent that makes your schema invalid, but your choice of relations.

from fluent.

patrickbrouwers avatar patrickbrouwers commented on July 2, 2024

If you actually want to do a one-to-one you should follow doctrine's guidelines for one-to-one (and ignore how Eloquent does it): http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-one-unidirectional

$user->hasOne(Setting::class)->... (or oneToOne)

and

$setting->hasOne(User::class)->... (or oneToOne)

from fluent.

matt-allan avatar matt-allan commented on July 2, 2024

Are you saying that hasOne and belongsTo should not be used for one-to-one relationships?

The point I was trying to make is that if you do create a one-to-one relationship using hasOne and belongsTo Doctrine's schema validation will always fail.

The reason I filed a bug report is the fluent docs state:

hasOne
Inspired by Laravel's relation syntax, this method maps a one-to-one inverse side relation.
belongsTo
Inspired by Laravel's relation syntax, this method maps the owning side of a one-to-one relation.

It sounds like you should be able to use hasOne and belongsTo to map a one-to-one relationship, and while it does work, it causes schema:validate to throw an error. The error is not because of my database schema; I am using --skip-sync. The error is because the mapping is considered invalid by doctrine.

from fluent.

patrickbrouwers avatar patrickbrouwers commented on July 2, 2024

@guiwoda can you have a look at this?

from fluent.

matt-allan avatar matt-allan commented on July 2, 2024

I pushed an example app to illustrate this issue: https://github.com/yuloh/lumen-app

It isn't clear if mappedBy and inversedBy are necessary for a one-to-one with these methods. If I leave them off I get this error:

$ php artisan doctrine:schema:validate --skip-sync

Validating for default entity manager...
[Mapping]  FAIL - The entity-class 'App\Entities\Scientist' mapping is invalid:

* The association App\Entities\Scientist#profile refers to the owning side field App\Entities\Profile#profile which does not exist.
Database] SKIPPED - The database was not checked for synchronicity.

Once they are added it returns this error:

$ php artisan doctrine:schema:validate --skip-sync

Validating for default entity manager...
[Mapping]  FAIL - The entity-class 'App\Entities\Profile' mapping is invalid:

* If association App\Entities\Profile#scientist is many-to-one, then the inversed side App\Entities\Scientist#profile has to be one-to-many.
Database] SKIPPED - The database was not checked for synchronicity.

I also added a test to illustrate that it does still work correctly.

from fluent.

guiwoda avatar guiwoda commented on July 2, 2024

@patrickbrouwers @yuloh Yes, indeed this is correct. The SchemaValidator expects a ONE_TO_ONE <> ONE_TO_ONE mapping, or a ONE_TO_MANY <> MANY_TO_ONE mapping. We are doing ONE_TO_ONE <> MANY_TO_ONE because on the implementation side, it doesn't matter. But the SchemaValidator is more strict than the implementation itself.

To allow a generic belongsTo semantic, we should parse the inverse side and decide to turn belongsTo into either oneToOne (owner) or manyToOne, just to please the SchemaValidator. In any case, you can avoid the belongsTo method if you need the validator to work.

from fluent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.