2017 © Pedro Peláez
 

yii2-extension yii2-profile

CRUD additional attributes without writing code.

image

shintio/yii2-profile

CRUD additional attributes without writing code.

  • Thursday, November 9, 2017
  • by AlekseyTopor
  • Repository
  • 3 Watchers
  • 5 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 25 % Grown

The README.md

Profile with additional attributes for ActiveRecord

Create, update, delete additional attributes of model without change model code., (*1)

Это руководство также доступно на русском языке., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

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

or add, (*5)

"shintio/yii2-profile": "*"

to the require section of your composer.json file., (*6)

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., (*7)

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., (*8)

namespace:, (*9)

use shintio\profile\Profile;

Create Profile object and ActiveRecord model by class name:, (*10)

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

Create Profile object by existing ActiveRecord:, (*11)

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

$profile=new Profile($user);

Find Profile:, (*12)

$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:, (*13)

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

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

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

echo '

';
// 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 '
'; $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., (*14)

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;

The Versions

09/11 2017

dev-master

9999999-dev

CRUD additional attributes without writing code.

  Sources   Download

GPL-3.0+

The Requires

 

by Shintio

extension yii2

09/11 2017

0.1

0.1.0.0

CRUD additional attributes without writing code.

  Sources   Download

GPL-3.0+

The Requires

 

by Shintio

extension yii2