• This repository has been archived on 10/Jan/2022
  • Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    PHP
  • License
    Other
  • Created about 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ActiveRecord behavior, which provides ability for custom records order setup

ActiveRecord Position Extension for Yii2


This extension provides support for ActiveRecord custom records order setup.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2tech/ar-position

or add

"yii2tech/ar-position": "*"

to the require section of your composer.json.

Usage

This extension provides support for custom records order setup via column-based position index.

This extension provides \yii2tech\ar\position\PositionBehavior ActiveRecord behavior for such solution support in Yii2. You may attach it to your model class in the following way:

<?php

use yii\db\ActiveRecord;
use yii2tech\ar\position\PositionBehavior;

class Item extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'positionBehavior' => [
                'class' => PositionBehavior::className(),
                'positionAttribute' => 'position',
            ],
        ];
    }
}

Behavior uses the specific integer field of the database entity to set up position index. Due to this the database entity, which the model refers to, must contain field positionAttribute.

In order to display custom list in correct order you should sort it by positionAttribute in ascending mode:

<?php

$records = Item::find()->orderBy(['position' => SORT_ASC])->all();
foreach ($records as $record) {
    echo $record->position . ', ';
}
// outputs: 1, 2, 3, 4, 5,...

Position saving

Being attached, behavior automatically fills up positionAttribute value for the new record, placing it to the end of the list:

<?php

echo Item::find()->count(); // outputs: 4

$item = new Item();
$item->save();

echo $item->position; // outputs: 5

However, you may setup position for the new record explicitly:

<?php

echo Item::find()->count(); // outputs: 4

$item = new Item();
$item->position = 2; // enforce position '2'
$item->save();

echo $item->position; // outputs: 2 !!!

Position switching

Existing record can be moved to another position using following methods:

  • movePrev() - moves record by one position towards the start of the list.
  • moveNext() - moves record by one position towards the end of the list.
  • moveFirst() - moves record to the start of the list.
  • moveLast() - moves record to the end of the list.
  • moveToPosition() - moves owner record to the specific position.

You may as well change record position through the attribute, provided to positionAttribute directly:

<?php

$item = Item::find()->andWhere(['position' => 3])->one();
$item->position = 5; // switch position to '5'
$item->save();

Position in group

Sometimes single database entity contains several listings, which require custom ordering, separated logically by grouping attributes. For example: FAQ questions may be grouped by categories, while inside single category questions should be ordered manually. For this case \yii2tech\ar\position\PositionBehavior::$groupAttributes can be used:

<?php

use yii\db\ActiveRecord;
use yii2tech\ar\position\PositionBehavior;

class FaqQuestion extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'positionBehavior' => [
                'class' => PositionBehavior::className(),
                'positionAttribute' => 'position',
                'groupAttributes' => [
                    'categoryId' // multiple lists varying by 'categoryId'
                ],
            ],
        ];
    }
}

In this case behavior will use owner values of groupAttributes as additional condition for position calculation and changing:

<?php

echo FaqQuestion::find()->andWhere(['categoryId' => 1])->count(); // outputs: '4'
echo FaqQuestion::find()->andWhere(['categoryId' => 2])->count(); // outputs: '7'

$record = new FaqQuestion();
$record->categoryId = 1;
$record->save();
echo $record->position; // outputs: '5'

$record = new FaqQuestion();
$record->categoryId = 2;
$record->save();
echo $record->position; // outputs: '8'

List navigation

Records with custom position order applied make a chained list, which you may navigate if necessary. You may use \yii2tech\ar\position\PositionBehavior::getIsFirst() and \yii2tech\ar\position\PositionBehavior::getIsLast() methods to determine if particular record is the first or last one in the list. For example:

<?php

echo Item::find()->count(); // outputs: 10

$firstItem = Item::find()->andWhere(['position' => 1])->one();
echo $firstItem->getIsFirst(); // outputs: true
echo $firstItem->getIsLast(); // outputs: false

$lastItem = Item::find()->andWhere(['position' => 10])->one();
echo $lastItem->getIsFirst(); // outputs: false
echo $lastItem->getIsLast(); // outputs: true

Having a particular record instance, you can always find record, which is located at next or previous position to it, using \yii2tech\ar\position\PositionBehavior::getNext() or \yii2tech\ar\position\PositionBehavior::getPrev() method. For example:

<?php

$item = Item::find()->andWhere(['position' => 5])->one();

$nextItem = $item->findNext();
echo $nextItem->position; // outputs: 6

$prevItem = $item->findPrev();
echo $prevItem->position; // outputs: 4

You may as well get the first and the last records in the list. For example:

<?php

echo Item::find()->count(); // outputs: 10
$item = Item::find()->andWhere(['position' => 5])->one();

$firstItem = $item->findFirst();
echo $firstItem->position; // outputs: 1

$lastItem = $item->findLast();
echo $lastItem->position; // outputs: 10

More Repositories

1

ar-softdelete

Soft delete behavior for ActiveRecord
PHP
202
star
2

crontab

Yii2 extension for crontab support
PHP
184
star
3

balance

Balance accounting (bookkeeping) system based on debit and credit principle
PHP
174
star
4

file-storage

This package has been abandoned. See: https://www.patreon.com/posts/59332556
PHP
119
star
5

admin

Admin pack (actions, widgets, etc) for Yii2
PHP
102
star
6

selfupdate

Basic script for project self update from VCS
PHP
92
star
7

csv-grid

Yii2 extension for CSV export
PHP
89
star
8

ar-linkmany

ActiveRecord behavior for saving many-to-many relations
PHP
87
star
9

illuminate

Yii2 to Laravel Migration Package
PHP
87
star
10

embedded

Support embedded models usage for complex ActiveRecord like MongoDB or ElasticSearch
PHP
81
star
11

spreadsheet

Yii2 extension for export to Excel
PHP
81
star
12

html2pdf

Yii2 component for HTML to PDF conversion
PHP
75
star
13

filedb

ActiveRecord for static data definitions based on files
PHP
73
star
14

config

Yii2 application runtime configuration support
PHP
62
star
15

sitemap

Site map creation support
PHP
59
star
16

project-template

Yii2 Project Template
PHP
54
star
17

content

Content management system for Yii2
PHP
53
star
18

ar-variation

Variation behavior for ActiveRecord
PHP
49
star
19

ar-dynattribute

Provide ActiveRecord dynamic attributes stored into the single field in serialized state
PHP
44
star
20

ar-file

This package has been abandoned. See: https://www.patreon.com/posts/59332556
PHP
36
star
21

ar-role

ActiveRecord behavior, which provides relation roles (table inheritance)
PHP
36
star
22

authlog

Identity auth tracking
PHP
33
star
23

ar-search

Provides unified search model for Yii ActiveRecord
PHP
31
star
24

ar-eagerjoin

ActiveRecord behavior, which provides relation eager loading by join without extra query
PHP
21
star
25

behavior-trait

Allows handling events via inline declared methods, which can be added by traits
PHP
20
star
26

install

basic script for project installation
PHP
18
star
27

activemail

Provides ActiveMessage mailing approach for Yii2
PHP
17
star
28

https

Secure connection (https) handling
PHP
11
star
29

model-change

Yii2 model data and state change tracking
PHP
11
star