-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hasManyToMany doesn't work as expected #938
Comments
https://github.com/charnad/phalcon-test I've set up a repository to illustrate my issue. |
Confirmed on Linux w/phalcon 1.2.1, x64 |
Looking into this. |
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM sqlite_master WHERE type='table' AND tbl_name='posts'
PRAGMA table_info('posts')
INSERT INTO "posts" ("title", "content", "id") VALUES (?, ?, null)
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM sqlite_master WHERE type='table' AND tbl_name='posts_tags'
PRAGMA table_info('posts_tags')
SELECT COUNT(*) "rowcount" FROM "posts_tags" WHERE "post_id" = ? AND "tag_id" = ?
INSERT INTO "posts_tags" ("post_id", "tag_id") VALUES (?, null) Looks like insertion into posts_tags happens too early — tags table needs to be updated first. |
$di->set('db', function() {
$em = new \Phalcon\Events\Manager();
$em->attach('db',
function($event, $conn)
{
if ($event->getType() == 'beforeQuery') {
echo $conn->getSQLStatement(), "\n";
}
}
);
$conn = new \Phalcon\Db\Adapter\Pdo\Sqlite(array("dbname" => __DIR__ . "/phalcon.sqlite"));
$conn->getInternalHandler()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$conn->setEventsManager($em);
return $conn;
}); INSERT INTO "posts" ("title", "content", "id") VALUES (?, ?, null)
SELECT COUNT(*) "rowcount" FROM "posts_tags" WHERE "post_id" = ? AND "tag_id" = ?
INSERT INTO "posts_tags" ("post_id", "tag_id") VALUES (?, null)
INSERT INTO "tags" ("tag", "id") VALUES (?, null) |
Tried 1.3.0 branch with fix #938 and it doesn't seem to fix the problem. |
Do you have a test case? |
I used both versions of phalcon the one from above and your fork with sql lite... it doesn't give the error - but it doesn't insert anything into posts_tags either. |
Did you use my fork of Phalcon or my fork of https://github.com/charnad/phalcon-test? The first one works, the second one does not as I fixed the C code, not PHP one. |
Do I get it right, that plain $tag = new Tag();
$post->tags = array($tag);
$post->save(); won't work without magic Model class? You said
but what is wrong with my code? It is almost identical to "official" examples. |
@charnad Please compile and install the latest 1.2.2 and your code will work. See this test case: https://github.com/phalcon/cphalcon/blob/1.2.2/unit-tests/ModelsRelationsTest.php#L324 |
Oh, I see. Misunderstood your comment. |
In 1.2.2 creating and updating entries works fine! Thank you. However one question left unclear. If I want to replace old relations with new ones, what should I do? $post->tags = array($tag1);
$post->save();
$post->tags = array($tag2);
$post->save(); I will have both tags saved (which is good) and both related to this post (which is not good). |
I would delete them first explicitly @phalcon What do you think? |
Well, I don't know if the ORM must implicitly delete them and replace with the new ones, that might not be obvious to many programmers deleting data permanently when the expected behavior may be another, ie. add more tags instead of replace them. What about delete them manually? $post->tags->delete();
$post->tags = array($tag1);
$post->save(); |
I wouldn't like to delete tags themselves, they might be in use with other posts. I just want to remove entries from the model in the middle (PostsTags). $post->tags->delete(); This will delete tags also, right? So do I need to define another relation just for PostsTags so that I could remove them? |
Sorry, I meant: $post->postsTags->delete(); My concern is about whether this behavior is the obvious behavior, replace vs. append. What a developer is expecting without read the docs, I think "append" is a safe behavior, something you can figure out without consequences. |
I understand your point, safer way is almost always better. Can I then please have an option for setting an alias for the model in the middle? Currently manyToMany is set: $this->hasManyToMany(
"id",
"Blog\\Model\\PostsTags",
"post_id",
"tag_id",
"Blog\\Model\\Tag",
"id",
array("alias" => "tags")
); I would like to have: - array("alias" => "tags")
+ array("alias" => "tags", "relationAlias" => "postsTags") |
You can set a hasMany relation to PostsTags: $this->hasManyToMany(
"id",
"Blog\\Model\\PostsTags",
"post_id",
array("alias" => "postsTags")
); |
Ok then. Thank you, I got the answers, so I close the issue. |
using PhalconPHP 1.3 , testing hasManyToMany relation Groups(m) <---- GroupsMenus --- > Menus(n) when editing, in relation table GroupsMenus create duplicate data, and also if we create our own behavior, eventType in behavior doesn't fire up| id | group_id | menu_id || 1 | 1 | 1 | <---- add new data here is my gist |
I'm trying to create a little test blog application. What I would like to achieve is:
Posts(m)---PostsTags---Tags(n)
Ordinary m-n relation, for which Model::hasManyToMany can be utilized.
I followed reference examples and ended up with this:
https://gist.github.com/charnad/b144d2b7664d4337f04f
I get: tag_id is required
If I save each tag separately before saving post, or use existing tags - only one PostTags entry is saved, for the last of the tags in the array.
If later I want to save a new array of tags instead of the old ones, old tags are not replaced, but remain and $post->tags return both old and new tags.
Phalcon version 1.2.0
The text was updated successfully, but these errors were encountered: