Skip to content
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

Fixed model event dispatcher does not works for Pivot and MorphPivot. #2427

Merged
merged 10 commits into from Sep 4, 2020
Merged

Fixed model event dispatcher does not works for Pivot and MorphPivot. #2427

merged 10 commits into from Sep 4, 2020

Conversation

weskiller
Copy link
Contributor

For example :

<?php


namespace App\Model\Test;

use Hyperf\DbConnection\Model\Relations\Pivot;
use Hyperf\DbConnection\Model\Model;
use Hyperf\ModelCache\CacheableInterface;
use Hyperf\ModelCache\Cacheable;

//用户
class User extends Model implements CacheableInterface
{
    use Cacheable;

    public function members()
    {
        $this->hasMany(Group::class);
    }
    public function groups()
    {
        $this->belongsToMany(Group::class, 'group_member')
            ->using(GroupMember::class)->as('member')
            ->withTimestamps();
    }
}

//群
class Group extends Model implements CacheableInterface
{
    use Cacheable;

    public function members()
    {
        $this->hasMany(GroupMember::class);
    }
    public function users()
    {
        $this->belongsToMany(User::class,'group_member')
            ->using(GroupMember::class)->as('member')
            ->withTimestamps();
    }
}


//群成员
class GroupMember extends Pivot implements CacheableInterface
{
    use Cacheable;

    public function user()
    {
        $this->belongsTo(GroupMember::class);
    }

    public function group()
    {
        $this->belongsTo(Group::class);
    }
}

//通过群成员ID查询成员
GroupMember::findFromCache(1);

@@ -14,10 +14,11 @@
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Model;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥要改成 Database 的 Model

@limingxinleo
Copy link
Member

@weskiller 我知道了,Cacheable无法触发是因为 EventDispatcher 的问题,而默认情况下,getConnection 是 OK的这里做了兼容。

但 EventDispatcher 没有做兼容,模型事件还是需要额外设置的。

而 Builder 相关的事件,在 getConnection 的时候就设置过了,所以没问题。

而 Model-Cache 是基于模型事件做的。这就导致 PIvot 获取监听器的时候拿不到 事先设置的默认值,导致无法生效。

@limingxinleo limingxinleo changed the title Fixed Pivot, MorphPivot are able to Cacheable Fixed model event dispatcher not works for Pivot and MorphPivot. Sep 4, 2020
@limingxinleo limingxinleo changed the title Fixed model event dispatcher not works for Pivot and MorphPivot. Fixed model event dispatcher does not works for Pivot and MorphPivot. Sep 4, 2020
@limingxinleo limingxinleo merged commit 30218c6 into hyperf:master Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants