Giter Site home page Giter Site logo

ActiveRecord "link" requires additional "save" when linked "viaTable" otherwise "Unable to link models: both models must NOT be newly created" error about active-record HOT 6 OPEN

yiisoft avatar yiisoft commented on June 18, 2024
ActiveRecord "link" requires additional "save" when linked "viaTable" otherwise "Unable to link models: both models must NOT be newly created" error

from active-record.

Comments (6)

dynasource avatar dynasource commented on June 18, 2024 3

:), if this was an algorithm, all the issues would have been closed at once

from active-record.

cebe avatar cebe commented on June 18, 2024

How do you expect the entry in the relation table to be created when the model has no primaryKey value assigned?

from active-record.

e-frank avatar e-frank commented on June 18, 2024

sure you are right. i wished i found some automagic here and expected a 'linked save' to save the complete model-tree. [or the same behaviour with and without ->viaTable. with viaTable the child model is saved, without isn't.]

when you use ->link a second time, you will get an additional row in the relation table, and no error, if you don't have an unique index.
i didn't find anything like a isLinked method, so what i think i have to do is test the parent relationship for an existing child by the child's primary key, like:

$hasChild = $parent->getRelation($relation)->andWhere('id=1')->exists();

and then decide if you need ->link.
i am working on a hydrator and hope to publish it asap.
still: fantastic work!

from active-record.

dynasource avatar dynasource commented on June 18, 2024

closed in favor of https://github.com/yiisoft/yii2/issues/4834

from active-record.

cebe avatar cebe commented on June 18, 2024

@dynasource this issue is #4834 :)

from active-record.

samdark avatar samdark commented on June 18, 2024

yiisoft/yii2#15882

from active-record.

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.