Coder Social home page Coder Social logo

Many to Many behaviour about phalcon HOT 32 OPEN

phalcon avatar phalcon commented on May 29, 2024
Many to Many behaviour

from phalcon.

Comments (32)

StudioMaX avatar StudioMaX commented on May 29, 2024 3

This is still relevant.

from phalcon.

StudioMaX avatar StudioMaX commented on May 29, 2024 2

Still doesn't work.

from phalcon.

fl0pp avatar fl0pp commented on May 29, 2024 1

I created the following method that I placed in my base model: http://pastebin.com/MikKbSFj

from phalcon.

prodigga avatar prodigga commented on May 29, 2024

Bump...

from phalcon.

prodigga avatar prodigga commented on May 29, 2024

Bump πŸ‘

from phalcon.

prodigga avatar prodigga commented on May 29, 2024

Bump :)

from phalcon.

 avatar commented on May 29, 2024

Not a bug, but probably something that can be improved with an array_unique

from phalcon.

firstactivemedia avatar firstactivemedia commented on May 29, 2024

@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.

lfbittencourt avatar lfbittencourt commented on May 29, 2024

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.

prodigga avatar prodigga commented on May 29, 2024

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.

lfbittencourt avatar lfbittencourt commented on May 29, 2024

@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.

prodigga avatar prodigga commented on May 29, 2024

@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.

Pajamaman avatar Pajamaman commented on May 29, 2024

@phalcon said:

Not a bug, but probably something that can be improved with an array_unique

Seems like a bug to me.

from phalcon.

sergeyklay avatar sergeyklay commented on May 29, 2024

@Pajamaman +1

from phalcon.

fl0pp avatar fl0pp commented on May 29, 2024

Have someone come up with a solution to this issue? It will do with plain PHP-code...

from phalcon.

prodigga avatar prodigga commented on May 29, 2024

This small issue steered me away from Phalcon a year ago.

from phalcon.

Jurigag avatar Jurigag commented on May 29, 2024

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.

fl0pp avatar fl0pp commented on May 29, 2024

@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.

makerlabs avatar makerlabs commented on May 29, 2024

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.

firstactivemedia avatar firstactivemedia commented on May 29, 2024

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.

angelvega93 avatar angelvega93 commented on May 29, 2024

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.

makerlabs avatar makerlabs commented on May 29, 2024

@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.

prodigga avatar prodigga commented on May 29, 2024

Lol. So, is phalcon dead?

from phalcon.

niden avatar niden commented on May 29, 2024

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.

angelvega93 avatar angelvega93 commented on May 29, 2024

This still not implemented?

from phalcon.

niden avatar niden commented on May 29, 2024

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.

niden avatar niden commented on May 29, 2024

Closing in favor of phalcon/cphalcon#13855. Will revisit if the community votes for it, or in later versions.

from phalcon.

StudioMaX avatar StudioMaX commented on May 29, 2024

Bump

from phalcon.

jturbide avatar jturbide commented on May 29, 2024

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 "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
  • 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.

jturbide avatar jturbide commented on May 29, 2024

https://github.com/zemit-cms/core/blob/master/src/Mvc/Model/Relationship.php

from phalcon.

stale avatar stale commented on May 29, 2024

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.

StudioMaX avatar StudioMaX commented on May 29, 2024

Still relevant

from phalcon.

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.