Comments (32)
This is still relevant.
from phalcon.
Still doesn't work.
from phalcon.
I created the following method that I placed in my base model: http://pastebin.com/MikKbSFj
from phalcon.
Bump...
from phalcon.
Bump π
from phalcon.
Bump :)
from phalcon.
Not a bug, but probably something that can be improved with an array_unique
from phalcon.
@phalcon - are you meaning an array_unique in the c code? Because if you mean in our php, I am a little unclear where it would go, as we are assigning an array with unique elements
from phalcon.
Talk about other frameworks here may be not too polite, but... Can't Phalcon have a simple sync
method like Laravel has?
You may also use the sync method to attach related models. The sync method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:
$user->roles()->sync(array(1, 2, 3));
Refer to http://laravel.com/docs/4.2/eloquent#inserting-related-models.
from phalcon.
I mean, sure, but its really not necessary if things worked correctly?
You could simply retreive your user model, and array push "role" entries into that "users" "roles" array. Or you could set the users roles array to an empty array (essentially removing all roles from the user), and array push roles 1 2 and 3 as with your example. Invoke save on the user and you are done..
Refer to my post here:
http://forum.phalconphp.com/discussion/2190/many-to-many-expected-behaviour#C12840
Am I the only one seeing how broken this all is? There seems to be so much confusion around this. None of this works as expected lol.
from phalcon.
@prodigga No, you're not. I agree with you :-)
My comment is just a potential next step, because there's no need to load your associated entities from database before attach them to your entity. I mean, in real life you don't SELECT id FROM table WHERE id = 1
. All you need are references, like Doctrine and Eloquent do.
from phalcon.
@lfbittencourt very good point! @phalcon just tagging phalcon to see if we can get a response, this issue is buried somewhere in the closed list
from phalcon.
@phalcon said:
Not a bug, but probably something that can be improved with an array_unique
Seems like a bug to me.
from phalcon.
@Pajamaman +1
from phalcon.
Have someone come up with a solution to this issue? It will do with plain PHP-code...
from phalcon.
This small issue steered me away from Phalcon a year ago.
from phalcon.
Well i will look into it when i have time, but to be honest i created service myself to create/update many to many relation records.
from phalcon.
@Jurigag could you please share that code with me? I consider doing it myself, but if you have already done it, I would be happy to use it!
from phalcon.
Wouldn't it be easier to just remove all the relations between the current post and tags and then to add them again this is how I normally deal with this kind of problem when it comes to many-to-many relations. Even when u look from the UI side its always either show all the related tags already added with an option to add another one. So either u use ajax and add only the one or two new tags or submit all the tags with a normal request again and use the method which I described above.
from phalcon.
Wouldn't it be easier to just remove all the relations between the current post and tags and then to add them again
The original post was more intended to find out of Phalcon is working as intended. Seems the official line is that it is, so no bug...?
Unsure if I have been spoilt by Doctrine doing more for me than phalcon, or fallen into some kind of anti-pattern / over-reliance on the ORM!
from phalcon.
I created the following method that I placed in my base model: http://pastebin.com/MikKbSFj
It works fine π but it would be even better if you could do like laravel 5
$article->sync('categories', [1, 2 => ['hidden' => 1], 3])
from phalcon.
@firstactivemedia sorry I was just referring to the resolution which @phalcon provided I wasn't talking about if its a bug or not :) But when u talk about object oriented approach than yeah I totally agree about the fact that if you assign an array to a object property it should be overriten like it would be in normal object.
from phalcon.
Lol. So, is phalcon dead?
from phalcon.
Not at all. We just don't have a lot of resources to fix everything. Some of the issues have been opened a long time ago and we have a bot that closes them automatically. For a good number of these issues they are either forgotten, fixed or not pursued. The rest of them we reopen them manually since they are indeed issues we need to address.
from phalcon.
This still not implemented?
from phalcon.
Some input please. Using the original example
$tag1 = \Tag::findFirst('id=1');
$tag2 = \Tag::findFirst('id=2');
$tag3 = \Tag::findFirst('id=3');
// create a post
$post = new \Post();
$post->tags = [$tag1, $tag2];
$post->save();
// Post is related to tag1 and tag2
// edit a post
$post = \Post::findFirst('id=1'); // the same post that was created earlier
$post->tags = [$tag1, $tag3];
$post->save();
// Post is related to tag1 and tag3
This is the intended behavior. If we were to take your approach and the edit action would append records, how would you be able to delete a relationship using edit? For instance based on your suggestion, the proposed functionality will give us:
// edit a post
$post = \Post::findFirst('id=1'); // the same post that was created earlier
$post->tags = [$tag1, $tag3];
$post->save();
// Post is related to tag1, tag2 and tag3
So the question is, how would you delete say tag1
from the relationship and "replace" it by say tag4
? If we do not use the direct assignment of what you need to save, then we would have to make an extra call to first delete what we do not want and then assign what we do.
Thoughts on this?
@prodigga @ghost @lfbittencourt @Pajamaman @fl0pp @Jurigag @makerlabs @angelvega93 @StudioMaX @firstactivemedia
from phalcon.
Closing in favor of phalcon/cphalcon#13855. Will revisit if the community votes for it, or in later versions.
from phalcon.
Bump
from phalcon.
This is what I am planning to do, the keepMissingRelationship toggle is bothering me. I already have a similar approach working in Phalcon 3.4. I am currently upgrading it to phalcon 4.
Let's say
- handling lots of relationships
- recursively saving nested entities indefinately
- belongsTo, hasOne, hasMany, hasManyToMany should behave the same
- matching using multi-field support for relationship binding + forced find condition
- handling model->assign (phalcon 4) or model->save (phalcon 3)
- allowing data manipulation for hasManyToMany nodes as well
- allowing whitelist and getColumns to be passed recursively for aliases as well
- Soft-delete friendlyness on relationships nodes
- Avoiding deleting or re-creating nodes, they could contain some persistant data as well (i.e position)
- Allowing many-to-many node data manipulation
- Allowing to pass a flag if you want to delete missing relationship or keep them
Saving?
- beging transaction
- save current model (we have to do it here because the primary keys could possibly be changed)
- get the relationships properties and values using the aliases
- find their primary keys (can we have combined keys in phalcon?)
- if primary key is not empty, fetch that entry
- if soft-delete behaviour, force soft-delete field to the softDelete->notDeleted value
- if entry can't be found, create new entry
- assign updated data and nested relationships to that entry
- if primary key is not empty, fetch that entry
- if "keepMissingRelationships" flag is false
- delete missing entries
- save and automagically save nested relationships recursively)
- forward children messages to parent model
- use alias to prefix fields
- find their primary keys (can we have combined keys in phalcon?)
- if error, rollback, commit otherwise
Assigning
$tagList = [];
$tagList []= true; // keep missing relationship flag could be passed here if we wanted to have different bahaviour at different nested levels
$tagList []= Tag::findById(1); // append or replace
$tagList []= ['name' => 'Tag 2']; // create and append
$tagList []= ['name' => 'Tag 3', 'PostNode' => ['position' => 3, 'Post' => ['name' => 'Post Name'], 'Tag' => ['name' => 'Tag 3!']]]; // create and append and then update node position
$tagList []= Tag::findById(4)->assign(['PostNode' => ['position' => 4]]);
$post->TagList = $tagList; // first way
$whiteList = ['TagList' => ['name' => true, 'PostNode' => ['position' => true]]];
$post->assign(['TagList' => $tagList], $whiteList); // second way, using nested whitelist
$keepMissingRelationships = true;
$model->save($keepMissingRelationships); // actually do shits using begin rollback & commits, also forward sub getMessages() to their parent model up to the main one
from phalcon.
https://github.com/zemit-cms/core/blob/master/src/Mvc/Model/Relationship.php
from phalcon.
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalcon.io/post/github-closing-old-issues
from phalcon.
Still relevant
from phalcon.
Related Issues (20)
- [NFR]: Replace regexp in the router with arrays for extra performance
- Can't access page, 404 FORBIDDEN
- Can I use `$this->getRelated` on already cached data?
- [BUG]: Scrutinizer Fixes HOT 1
- [NFR]: ADD sticky for read write connection
- [NFR]: removeBehavior method in Model
- [NFR]:returnedValue not updated after dispatcher->forward
- [NFR]: No way to add html attributes to Select Tag. documentation inadequate HOT 1
- [BUG]: The "setDefault" method does not work for forms of type "text" with name "value" HOT 5
- update src folder links
- How to use cookie in phalcon 4.x ? HOT 1
- update tests folder links
- [NFR]: Complete rework of ORM HOT 3
- [BUG]: \Phalcon\Encryption\Crypt + named parameters/arguments throw fatal error. HOT 1
- [NFR]: Refactor Phalcon\Mvc\Router/Route HOT 1
- [NFR]: Refactor Phalcon\Mvc\Model\MetaData
- phalcon 3,4 webhook problem HOT 2
- [NFR]: What is the purpose of this libraryοΌ HOT 3
- [NFR]: Add a getResult() or fetchAll() method to Resultset\Simple
- [BUG]: when i use JWT Builder , api returns blank HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phalcon.