forked from sjaakp/yii2-sortable-behavior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMSortable.php
62 lines (54 loc) · 1.56 KB
/
MMSortable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* MIT licence
* Version 1.0
* Sjaak Priester, Amsterdam 28-08-2014.
*/
namespace sjaakp\sortable;
use yii\base\Behavior;
use yii\helpers\StringHelper;
use yii\helpers\Inflector;
use yii\db\ActiveRecord;
/**
* Class MMSortable
* @package sjaakp\sortable
* @var $owner ActiveRecord
*/
class MMSortable extends Behavior {
/**
* @var string - fully qualified class name of the pivot class.
*/
public $pivotClass;
/**
* @var string - attribute name of the owner's id in the pivot class
* If this is not set, it will be derived from the owner's class name;
* for instance: if the owner is class Movie, $pivotIdAttr will be "movie_id".
*/
public $pivotIdAttr;
public function events() {
return [
ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
];
}
protected function idAttr() {
if (is_null($this->pivotIdAttr)) {
$owner = $this->owner;
$this->pivotIdAttr = Inflector::camel2id(StringHelper::basename($owner->className()), '_') . '_id';
}
return $this->pivotIdAttr;
}
public function getPivots() {
/**
* @var $owner ActiveRecord
*/
$owner = $this->owner;
return $owner->hasMany($this->pivotClass, [$this->idAttr() => $owner->primaryKey()[0]]);
}
public function beforeDelete($event) {
/**
* @var $piv ActiveRecord
*/
// Make sure PivotRecord::beforeDelete is called
foreach ($this->getPivots()->all() as $piv) $piv->delete();
}
}