定制 shintio/yii2-profile 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

shintio/yii2-profile

Composer 安装命令:

composer require shintio/yii2-profile

包简介

CRUD additional attributes without writing code.

README 文档

README

Create, update, delete additional attributes of model without change model code.

Это руководство также доступно на русском языке.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require shintio/yii2-profile "*"

or add

"shintio/yii2-profile": "*"

to the require section of your composer.json file.

Before start

Before start you should create tables in your db. You can find SQL query in the end of this file. After create tables you should create or generate with gii ActiveRecord models for these tables.

Usage

For adding additional fields you should insert row to table 'enitity_profile_field'.

  • code- string on which you can use this field.
  • name- string, Human-Understandable name of this field. Use this for display field name to users.
  • type- json, type of this field. This version only supports {"type":"text"}. More types in future updates.

namespace:

use shintio\profile\Profile;

Create Profile object and ActiveRecord model by class name:

// User- ActiveRecord model
$profile=new Profile(User::className());

Create Profile object by existing ActiveRecord:

// We can create new User;
$user=new User();
// Also we can use existing User;
//$user=User::find()->one();

$profile=new Profile($user);

Find Profile:

$query=Profile::find(User::className());

// In where function we can use:
// Assoc array for '=' condition
$query->where(['username'=>'admin']); // username- User's attribute
// Indexed array for '%LIKE%' condition
$query->andWhere(['LIKE','firstName','Adm']); // firstName- additional profile field
$query->orWhere(['lastName'=>'Great']); // lastName- additional profile field

$profile=$query->one();
//$profiles=$query->all();

Work with fields:

echo $profile->getField('firstName'); // Admin

$profile->setField('firstName','Moder');
echo $profile->getField('firstName'); // Moder

$profile->setField('username','moder');

echo '<pre>';
// Get profile in array of Field ojbects
var_dump($profile->getProfile());
// Get profile in assoc array
// var_dump($profile->getProfile(true));
// Same as getProfile(true)
// var_dump($profile->getProfileInArray());
echo '</pre>';

$profile->save();
// username: moder
// firstName: Moder
// lastName: Great

SQL for generate example tables you can find in file example.sql Also you can you this template for create table for any entities. Before executing this query replace all 'entity' to your entity name.

CREATE TABLE `entity` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `entity_profile` (
  `id` int(11) NOT NULL,
  `entity_id` int(11) NOT NULL,
  `field_id` int(11) NOT NULL,
  `value` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `entity_profile_field` (
  `id` int(11) NOT NULL,
  `code` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `type` json NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `entity`
  ADD PRIMARY KEY (`id`);
  
ALTER TABLE `entity_profile`
  ADD PRIMARY KEY (`id`),
  ADD KEY `entity_id` (`entity_id`),
  ADD KEY `field_id` (`field_id`);

ALTER TABLE `entity_profile_field`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `entity_profile`
  ADD CONSTRAINT `entity_profile_ibfk_1` FOREIGN KEY (`field_id`) REFERENCES `entity_profile_field` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `entity_profile_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entity` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

统计信息

  • 总下载量: 42
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2017-11-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固