Skip to content

Added onCondition and where condition for the FK delete queries #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f4ef3b4
Added onCondition and where condition for the FK delete queries
deadmantfa Feb 8, 2018
486fcf3
Publishing to composer
deadmantfa Feb 9, 2018
a3a450a
Publishing to composer
deadmantfa Feb 9, 2018
ebedf91
Publishing to composer
deadmantfa Feb 9, 2018
9f22176
Publishing to composer
deadmantfa Feb 9, 2018
cd084db
Publishing to composer
deadmantfa Feb 9, 2018
f988921
Publishing to composer
deadmantfa Feb 9, 2018
6bd3a6a
Publishing to composer
deadmantfa Feb 9, 2018
7d39ff4
Publishing to composer
deadmantfa Feb 9, 2018
3899b65
Minor fix
deadmantfa Feb 11, 2018
2192d3e
Minor fix
deadmantfa Feb 11, 2018
551a47e
Minor fix
deadmantfa Feb 11, 2018
98ab3c8
Minor fix
deadmantfa Feb 14, 2018
7e2e5e1
Minor fix
deadmantfa Feb 14, 2018
2aa986e
Minor fix tag issue
deadmantfa Feb 14, 2018
159ddf7
🔧 (gitignore): add .idea and vendor to ignore IDE and dependency files
deadmantfa Jan 6, 2025
906d582
📝 (README.md): update installation instructions to specify stable ver…
deadmantfa Jan 6, 2025
7f1146c
📝 (RelationTrait.php): remove return type annotations from docblocks …
deadmantfa Jan 13, 2025
8ac396a
♻️ (RelationTrait.php): remove unnecessary else statements to simplif…
deadmantfa Jan 14, 2025
3121d05
♻️ (RelationTrait.php): replace empty() checks with strict array comp…
deadmantfa Jan 14, 2025
7b91286
🔧 (rector.php): increase type coverage, dead code, and code quality l…
deadmantfa Jan 14, 2025
e6fd00d
💡 (RelationTrait.php): convert line endings from LF to CRLF for consi…
deadmantfa Jan 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/nbproject/*
/nbproject/*
.idea
vendor
293 changes: 104 additions & 189 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,236 +1,151 @@
# yii2-relation-trait
Yii 2 Models add functionality for load with relation (loadAll($POST)), & transactional save with relation (saveAll())

PLUS soft delete/restore feature!
> **Note**: This is **not** the official extension by [@mootensai](https://github.com/mootensai).
> I am not the creator of the original extension. I have made bug fixes and improvements that suit my use case.
> Feel free to use it or refer to the official package
> at [mootensai/yii2-relation-trait](https://github.com/mootensai/yii2-relation-trait).

Best work with [mootensai/yii2-enhanced-gii](https://github.com/mootensai/yii2-enhanced-gii)
Yii 2 Models add functionality for loading related models via `loadAll($POST)` and transactional saving via
`saveAll()`.
It also supports **soft delete** and **soft restore** features.

[![Latest Stable Version](https://poser.pugx.org/mootensai/yii2-relation-trait/v/stable)](https://packagist.org/packages/mootensai/yii2-relation-trait)
[![License](https://poser.pugx.org/mootensai/yii2-relation-trait/license)](https://packagist.org/packages/mootensai/yii2-relation-trait)
[![Total Downloads](https://img.shields.io/packagist/dt/mootensai/yii2-relation-trait.svg?style=flat-square)](https://packagist.org/packages/mootensai/yii2-relation-trait)
[![Monthly Downloads](https://poser.pugx.org/mootensai/yii2-relation-trait/d/monthly)](https://packagist.org/packages/mootensai/yii2-relation-trait)
[![Daily Downloads](https://poser.pugx.org/mootensai/yii2-relation-trait/d/daily)](https://packagist.org/packages/mootensai/yii2-relation-trait)
[![Join the chat at https://gitter.im/mootensai/yii2-relation-trait](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mootensai/yii2-relation-trait?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Works best with [mootensai/yii2-enhanced-gii](https://github.com/mootensai/yii2-enhanced-gii).

## Support
## Badges

[![Support via Gratipay](https://cdn.rawgit.com/gratipay/gratipay-badge/2.3.0/dist/gratipay.svg)](https://gratipay.com/mootensai/)

https://www.paypal.me/yohanesc

Endorse me on LinkedIn

https://www.linkedin.com/in/yohanes-candrajaya-b68394102/
[![Latest Stable Version](https://poser.pugx.org/deadmantfa/yii2-relation-trait/v/stable)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)
[![License](https://poser.pugx.org/deadmantfa/yii2-relation-trait/license)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)
[![Total Downloads](https://img.shields.io/packagist/dt/deadmantfa/yii2-relation-trait.svg?style=flat-square)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)
[![Monthly Downloads](https://poser.pugx.org/deadmantfa/yii2-relation-trait/d/monthly)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)
[![Daily Downloads](https://poser.pugx.org/deadmantfa/yii2-relation-trait/d/daily)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)

## Installation

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).

Either run

```bash
$ composer require 'mootensai/yii2-relation-trait:dev-master'
composer require deadmantfa/yii2-relation-trait
```

or add

```php
"deadmantfa/yii2-relation-trait": "^2.0.0"
```
"mootensai/yii2-relation-trait": "*"
```

to the `require` section of your `composer.json` file.

to the require section of your application's ```composer.json``` file.

## Usage At Model
## Usage in the Model

```php
class MyModel extends ActiveRecord{
use \mootensai\relation\RelationTrait;
}
```

## Array Input & Usage At Controller
use deadmantfa\relation\RelationTrait;

It takes a normal array of POST. This is the example
```php
Array (
$_POST['ParentClass'] => Array
(
[attr1] => value1
[attr2] => value2
// has many
[relationName] => Array
(
[0] => Array
(
[relAttr] => relValue1
)
[1] => Array
(
[relAttr] => relValue1
)
)
// has one
[relationName] => Array
(
[relAttr1] => relValue1
[relAttr2] => relValue2
)
)
)

OR

Array (
$_POST['ParentClass'] => ['attr1' => 'value1','attr2' => 'value2'],
// Has One
$_POST['RelatedClass'] => ['relAttr1' => 'value1','relAttr2' => 'value2'],
// Has Many
$_POST['RelatedClass'] => Array
(
[0] => Array
(
[attr1] => value1
[attr2] => value2
)
[1] => Array
(
[attr1] => value1
[attr2] => value2
)
)
)
```
class MyModel extends \yii\db\ActiveRecord
{
use RelationTrait;

```php
// sample at controller
if($model->loadAll(Yii:$app->request->post()) && $model->saveAll()){
return $this->redirect(['view', 'id' => $model->id, 'created' => $model->created]);
// ...
}
```

# Features
## Controller Usage

## Array Output
The extension expects a **normal array of POST** data. For example:

```php
// I use this to send model & related through JSON / Serialize
print_r($model->getAttributesWithRelatedAsPost());
```

```
Array
(
[MainClass] => Array
(
[attr1] => value1
[attr2] => value2
)

[RelatedClass] => Array
(
[0] => Array
(
[attr1] => value1
[attr2] => value2
)
)

)
```
[
$_POST['ParentClass'] => [
'attr1' => 'value1',
'attr2' => 'value2',
// Has many
'relationName' => [
[ 'relAttr' => 'relValue1' ],
[ 'relAttr' => 'relValue2' ]
],
// Has one
'relationName' => [
'relAttr1' => 'relValue1',
'relAttr2' => 'relValue2'
]
]
];
```

In your controller:

```php
print_r($model->getAttributesWithRelated());
```

```
Array
(
[attr1] => value1
[attr2] => value2
[relationName] => Array
(
[0] => Array
(
[attr1] => value1
[attr2] => value2
)
)
)
```

## Use Transaction

So your data will be atomic
(see : http://en.wikipedia.org/wiki/ACID)

## Use Normal Save

So your behaviors still works

## Add Validation At Main Model

```php
$form->errorSummary($model);
```

will give you

```
<<Related Class Name>> #<<index + 1>> : <<error message>>
My Related Model #1 : Attribute is required
$model = new ParentClass();
if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
return $this->redirect(['view', 'id' => $model->id]);
}
```

## It Works On Auto Incremental PK Or Not (I Have Tried Use UUID)

See here if you want to use my behavior :

https://github.com/mootensai/yii2-uuid-behavior

## Soft Delete

Add this line to your Model to enable soft delete
Features

1. Transaction Support
a. Your data changes are atomic (ACID compliant).
2. Normal ```save()```
a. Behaviors still work as usual since it’s built on top of Yii’s ```ActiveRecord```.
3. Validation
a. Errors from related models appear via ```errorSummary()```, e.g.
```text
MyRelatedClass #2: [Error message]
```
4. UUID or Auto-Increment
Works with any PK strategy,
including [mootensai/yii2-uuid-behavior](https://github.com/mootensai/yii2-uuid-behavior).
5. Soft Delete
By defining ```$_rt_softdelete``` in your model constructor (and ```$_rt_softrestore``` for restoring), you can
softly mark rows as deleted instead of physically removing them.
```php
private $_rt_softdelete;
private $_rt_softrestore;

public function __construct($config = [])
{
parent::__construct($config);

$this->_rt_softdelete = [
'is_deleted' => 1,
'deleted_by' => Yii::$app->user->id,
'deleted_at' => date('Y-m-d H:i:s'),
];

$this->_rt_softrestore = [
'is_deleted' => 0,
'deleted_by' => null,
'deleted_at' => null,
];
}
```

## Array Outputs

```php
private $_rt_softdelete;

function __construct(){
$this->_rt_softdelete = [
'<column>' => <undeleted row marker value>
// multiple row marker column example
'isdeleted' => 1,
'deleted_by' => \Yii::$app->user->id,
'deleted_at' => date('Y-m-d H:i:s')
];
}
print_r($model->getAttributesWithRelatedAsPost());
```

Add this line to your Model to enable soft restore
Produces a POST-like structure with the main model and related arrays.

```php
private $_rt_softrestore;

function __construct(){
$this->_rt_softrestore = [
'<column>' => <undeleted row marker value>
// multiple row marker column example
'isdeleted' => 0,
'deleted_by' => 0,
'deleted_at' => 'NULL'
];
}
print_r($model->getAttributesWithRelated());
```

### Should work on Yii's supported DB

It use all Yii's Active Query or Active Record to execute DB command


### I'm open for any improvement
Please create issue if you got a problem or an idea for enhancement
Produces a nested structure under ```[relationName] => [...]```.

#### ~ SDG ~
## Contributing or Reporting Issues

Please open an [issue](https://github.com/deadmantfa/yii2-relation-trait/pulls) or submit a PR if you find a bug or have
an improvement idea.

---

### Disclaimer

This package is a **fork** or an alternative
to [mootensai/yii2-relation-trait](https://github.com/mootensai/yii2-relation-trait).
**All credit** to [@mootensai](https://github.com/mootensai) for the initial code.
**This is not meant to replace** the original package but rather provide bug fixes and enhancements under a different
namespace.
Loading