indigerd/yii2-embedded-models

为 yii2 框架提供的嵌入式模型的活化和提取行为

v0.0.5 2019-06-25 15:23 UTC

This package is auto-updated.

Last update: 2024-09-26 03:07:42 UTC


README

用法

为了使用嵌入式模型将其他模型嵌入到您的模型中,您应该从库提供的模型扩展它。使用 HydrateBehavior 将单个模型嵌入到属性中或使用 HydrateCollectionBehavior 将模型集合嵌入到属性中。要正确附加行为,您必须为以下属性提供有效的配置值

  • attribute 您希望嵌入的属性的主模型名称
  • targetModel 嵌入模型类的名称
  • hydrator 用于活化和提取的 Hydrator。如果您已在 DI 容器中配置了 hydrator,则可以提供类名称;对于 Yii::createObject,提供数组定义;或提供 Hydrator 的实例

您的嵌入式模型也可以嵌入其他模型。当对您的主体模型调用验证时,也会对您的嵌入式模型以及主体模型中的相应嵌入模型字段调用验证。嵌入模型将自动由行为在从数据库填充您的模型或使用 Model::load() 从请求中填充数据时创建。要使用 Model::load() 自动填充嵌入模型字段,您需要将此字段添加到 rules() 作为 safe

将行为附加到 mongodb ActiveRecord 模型的示例

# configure default Hydrator object in your DI

Yii::$container->set(
    'Indigerd\Hydrator\Accessor\AccessorInterface',
    'Indigerd\Hydrator\Accessor\PropertyAccessor'
);

Yii::$container->set(
    'Indigerd\Hydrator\Hydrator'
);

# Primary ("parent") model

use Indigerd\Hydrator\Hydrator;
use indigerd\embedded\behavior\HydrateBehavior;
use indigerd\embedded\behavior\HydrateCollectionBehavior;
use indigerd\embedded\model\mongodb\ActiveRecord;

class Clinic extends ActiveRecord
{
    public static function collectionName(): string
    {
        return 'clinics';
    }

    public function attributes(): array
    {
        return [
            '_id',
            'name',
            'country',
            'doctors',
        ];
    }

    public function behaviors() : array
    {
        return [
            [
                'class' => HydrateBehavior::class,
                'hydrator' => Hydrator::class,
                'targetModel' => Country::class,
                'attribute' => 'country'
            ],
            [
                'class' => HydrateCollectionBehavior::class,
                'hydrator' => Hydrator::class,
                'targetModel' => Doctor::class,
                'attribute' => 'doctors'
            ],
        ];
    }
    
}

# Country model

use yii\base\Model;

class Country extends Model
{
    protected $name;
    
    protected $code;
    
    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setCode($code)
    {
        $this->code = $code;
    }

    public function getCode()
    {
        return $this->code;
    }
    
    public function fields() : array 
    {
        return [
            'name',
            'code'
        ];
    }    
}


# Doctor model

use indigerd\embedded\model\Model;

class Doctor extends Model
{
    protected $name;
    
    protected $contact;
    
    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setContact(Contact $contact)
    {
        $this->contact = $contact;
    }

    public function getContact()
    {
        return $this->contact;
    }
    
    public function fields() : array 
    {
        return [
            'name',
            'contact'
        ];
    }    

    public function behaviors() : array
    {
        return [
            [
                'class' => HydrateBehavior::class,
                'hydrator' => Hydrator::class,
                'targetModel' => Contact::class,
                'attribute' => 'contact'
            ],
        ];
    }
}


# Contact model

use yii\base\Model;

class Contact extends Model
{
    protected $phone;
    
    protected $email;
    
    public function setPhone($phone)
    {
        $this->phone = $phone;
    }

    public function getPhone()
    {
        return $this->phone;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getEmail()
    {
        return $this->email;
    }
    
    public function fields() : array 
    {
        return [
            'phone',
            'email'
        ];
    }    
}