top-think / think-orm

Think ORM——the PHP Database&ORM Framework
Apache License 2.0
413 stars 171 forks source link

我如何遍历模型的所有数据包括with的数据 #605

Closed netmou closed 1 month ago

netmou commented 1 month ago

我想在model的基类用onAfterRead 事件去处理所有查询到的id,主表的id可以通过getData获得,但是 $row = EmailReceiver::with('sender') ->where($where) ->find(); 像这样的width查询我就无法遍历了,因为在基类无法得知子类有个sender数据,我应当怎么做呢,我看到model\concern\Attribute 有一个私有变量$get存放着获取器数据,但是外边无法直接使用这个变量,能否增加方法获取$get的值呢

big-dream commented 1 month ago

可以通过 $row->sender 去获取关联模型 sender 的数据。

netmou commented 1 month ago

在基类我不知道有sender这个东西啊

netmou commented 1 month ago

基类做的是通用的事情

big-dream commented 1 month ago

可以用 getRelation() 方法:

$info = Orders::with(['user'])->where('id', 1)->find();
dump($info->getRelation()); // ['user' => ...]
netmou commented 4 weeks ago

/**
 * 自定义模型基类
 */
class BaseModel extends Model
{

  // ....

    public  static  function onAfterRead($model){
        file_put_contents('111112223.txt',"\r\n----\r\n".var_export($model->getRelation(),true),FILE_APPEND);
        ;
        //exit;
    }

}

onAfterRead 获取不到数据

big-dream commented 3 weeks ago

应该是触发 onAfterRead 时,还未查询关联模型的数据