riesenia/persist-related-data 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

riesenia/persist-related-data

最新稳定版本:v2.0.0

Composer 安装命令:

composer require riesenia/persist-related-data

包简介

CakePHP ORM plugin for persisting selected fields of related tables

README 文档

README

Build Status Latest Version Total Downloads Software License

This plugin is for CakePHP 4.x and contains behavior that handles saving selected fields of related data (redundantly).

Installation

Update composer.json file to include this plugin

{
    "require": {
        "riesenia/persist-related-data": "~1.0"
    }
}

Load plugin in config/bootstrap.php

Plugin::load('PersistRelatedData');

Usage

Good example for using this behavior is Invoices model that is related to Contacts. You can provide select box with contacts and save only contact_id when creating new invoice. But when contact data are modified later, your invoice should be left intact.

Example below assumes the invoices table has fields contact_id, contact_name and contact_address, while the contacts table has fields name and address. When you save Invoice entity with provided contact_id, fields contact_name and contact_address will be filled automatically.

class InvoicesTable extends Table
{
    public function initialize(array $config): void
    {
        parent::initialize($config);

        // add PersistRelatedData behavior
        $this->addBehavior('PersistRelatedData.PersistRelatedData', [
            'fields' => [
                'contact_name' => 'Contacts.name',
                'contact_address' => 'Contacts.address'
            ]
        ]);

        // associations
        $this->belongsTo('Contacts', [
            'foreignKey' => 'contact_id',
            'className' => 'Contacts'
        ]);
    }
}

Changeable fields

By default, all fields are automatically overwritten with related data when the foreign key changes. If you want to allow manual edits to persisted fields while still populating them automatically when empty, use the changeable configuration option:

$this->addBehavior('PersistRelatedData.PersistRelatedData', [
    'fields' => [
        'contact_name' => 'Contacts.name',
        'contact_address' => 'Contacts.address'
    ],
    'changeable' => [
        'contact_name'  // this field can be manually edited
    ]
]);

With this configuration:

  • contact_name will be populated from related data only if it's empty
  • contact_address will always be overwritten with related data when the foreign key changes

统计信息

  • 总下载量: 39.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-10