Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79a0aa6
commit 03575fd
Showing
12 changed files
with
996 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Tutoriais | ||
|
||
### Working with relational removals by yii2 | ||
|
||
This tutorial shows you how to safely and safely remove relationships between relationships and multiple lines. | ||
|
||
First we create three tables using a Many to Many relationship. | ||
![](relacao-exemplo.jpg) | ||
|
||
** Important: Apply cascate to foreign key, update and Delete. ** | ||
|
||
This feature will allow you to remove the foreign keys along with the line you want to delete. This is very useful not to pollute your code. | ||
|
||
Script Sql the ralation has. | ||
|
||
```Sql | ||
CREATE TABLE `tests_has_games` ( | ||
`tests_id` int(11) NOT NULL, | ||
`games_id` int(11) NOT NULL, | ||
PRIMARY KEY (`tests_id`,`games_id`), | ||
KEY `fk_tests_has_games_games1_idx` (`games_id`), | ||
KEY `fk_tests_has_games_tests_idx` (`tests_id`), | ||
CONSTRAINT `fk_tests_has_games_games1` FOREIGN KEY (`games_id`) REFERENCES `games` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, | ||
CONSTRAINT `fk_tests_has_games_tests` FOREIGN KEY (`tests_id`) REFERENCES `tests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin | ||
|
||
``` | ||
|
||
* Step 2: Create models: (to use generator gii, this system create model with relations of the table). | ||
|
||
** ModelGames: ** | ||
|
||
```php | ||
public function getTestsHasGames() | ||
{ | ||
return $this->hasMany(TestsHasGames::className(), ['games_id' => 'id']); | ||
} | ||
|
||
/** | ||
* @return \yii\db\ActiveQuery | ||
*/ | ||
public function getTests() | ||
{ | ||
return $this->hasMany(Tests::className(), ['id' => 'tests_id'])->viaTable('{{%tests_has_games}}', ['games_id' => 'id']); | ||
} | ||
|
||
``` | ||
|
||
** Model Tests: ** | ||
|
||
```php | ||
public function getTestsHasGames() | ||
{ | ||
return $this->hasMany(TestsHasGames::className(), ['tests_id' => 'id']); | ||
} | ||
|
||
/** | ||
* @return \yii\db\ActiveQuery | ||
*/ | ||
public function getGames() | ||
{ | ||
return $this->hasMany(Games::className(), ['id' => 'games_id'])->viaTable('{{%tests_has_games}}', ['tests_id' => 'id']); | ||
} | ||
|
||
```` | ||
|
||
* Step 3: Simple Removal Functions in controllers: | ||
|
||
```php | ||
public function actionDeletarTest($id) | ||
{ | ||
|
||
$session = \Yii::$app->session; | ||
|
||
$testes = Tests::findOne($id); | ||
|
||
if($testes->delete()) | ||
{ | ||
$session->addFlash('resposta', [ | ||
'type'=>'success', | ||
'msg'=>'Teste deletado com sucesso.' | ||
]); | ||
}else{ | ||
$session->addFlash('resposta', [ | ||
'type'=>'danger', | ||
'msg'=>'Erro encontrado favor resolver.' | ||
]); | ||
} | ||
|
||
return $this->redirect(['site/delete-relations']); | ||
|
||
} | ||
|
||
``` | ||
|
||
* Other Methods | ||
|
||
```php | ||
|
||
|
||
public function actionDeleteListGames() | ||
{ | ||
$array_id = [1,2,3,4]; | ||
|
||
$games = Games::find()->where(['in','id',$array_id])->all(); | ||
|
||
foreach($games as $k => $game) | ||
{ | ||
$game->delete(); | ||
} | ||
|
||
} | ||
|
||
|
||
public function actionDeleteTheRelation($id) | ||
{ | ||
|
||
|
||
$game = Games::find()->joinWith(['tests'=>function($q){ | ||
return $q->where(['like','nome','proccess']) | ||
}])->where(['id'=>$id])->one(); | ||
|
||
foreach($games->tests as $k => $test) | ||
{ | ||
$test->delete(); | ||
} | ||
|
||
} | ||
|
||
``` | ||
|
||
** All code in this post ** | ||
|
||
@author André Luiz Pereira | ||
Link: https://www.facebook.com/androidemastercode/ |
212 changes: 212 additions & 0 deletions
212
yii-trabalhando-com-remocoes-relacionais/code/controllers/SiteController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
<?php | ||
|
||
namespace app\controllers; | ||
|
||
use Yii; | ||
use yii\filters\AccessControl; | ||
use yii\web\Controller; | ||
use yii\filters\VerbFilter; | ||
use app\models\LoginForm; | ||
use app\models\ContactForm; | ||
use app\models\Tests; | ||
use app\models\Games; | ||
|
||
class SiteController extends Controller | ||
{ | ||
|
||
|
||
|
||
public function actionDeleteRelations() | ||
{ | ||
|
||
$testes = Tests::search(); | ||
$games = Games::search(); | ||
|
||
return $this->render('delete',[ | ||
'testes'=>$testes, | ||
'games'=>$games, | ||
]); | ||
} | ||
|
||
public function actionDeletarTest($id) | ||
{ | ||
|
||
$session = \Yii::$app->session; | ||
|
||
$testes = Tests::findOne($id); | ||
|
||
if($testes->delete()) | ||
{ | ||
$session->addFlash('resposta', [ | ||
'type'=>'success', | ||
'msg'=>'Teste deletado com sucesso.' | ||
]); | ||
}else{ | ||
$session->addFlash('resposta', [ | ||
'type'=>'danger', | ||
'msg'=>'Erro encontrado favor resolver.' | ||
]); | ||
} | ||
|
||
return $this->redirect(['site/delete-relations']); | ||
|
||
} | ||
|
||
public function actionDeletarGame($id) | ||
{ | ||
|
||
$games = Games::findOne($id); | ||
$session = \Yii::$app->session; | ||
|
||
if($games->delete()) | ||
{ | ||
$session->addFlash('resposta', [ | ||
'type'=>'success', | ||
'msg'=>'Tabela deletada com sucesso!' | ||
]); | ||
}else{ | ||
$session->addFlash('resposta', [ | ||
'type'=>'danger', | ||
'msg'=>'Ocorreu um erro no processo de remoção' | ||
]); | ||
} | ||
|
||
return $this->redirect(['site/delete-relations']); | ||
} | ||
|
||
public function actionDeleteListGames() | ||
{ | ||
$array_id = [1,2,3,4]; | ||
|
||
$games = Games::find()->where(['in','id',$array_id])->all(); | ||
|
||
foreach($games as $k => $game) | ||
{ | ||
$game->delete(); | ||
} | ||
|
||
} | ||
|
||
public function actionDeleteTheRelation($id) | ||
{ | ||
|
||
|
||
$game = Games::find()->joinWith(['tests'=>function($q){ | ||
return $q->where(['like','nome','proccess']) | ||
}])->where(['id'=>$id])->one(); | ||
|
||
foreach($games->tests as $k => $test) | ||
{ | ||
$test->delete(); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function behaviors() | ||
{ | ||
return [ | ||
'access' => [ | ||
'class' => AccessControl::className(), | ||
'only' => ['logout'], | ||
'rules' => [ | ||
[ | ||
'actions' => ['logout'], | ||
'allow' => true, | ||
'roles' => ['@'], | ||
], | ||
], | ||
], | ||
'verbs' => [ | ||
'class' => VerbFilter::className(), | ||
'actions' => [ | ||
'logout' => ['post'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function actions() | ||
{ | ||
return [ | ||
'error' => [ | ||
'class' => 'yii\web\ErrorAction', | ||
], | ||
'captcha' => [ | ||
'class' => 'yii\captcha\CaptchaAction', | ||
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Displays homepage. | ||
* | ||
* @return string | ||
*/ | ||
public function actionIndex() | ||
{ | ||
return $this->render('index'); | ||
} | ||
|
||
/** | ||
* Login action. | ||
* | ||
* @return string | ||
*/ | ||
public function actionLogin() | ||
{ | ||
if (!Yii::$app->user->isGuest) { | ||
return $this->goHome(); | ||
} | ||
|
||
$model = new LoginForm(); | ||
if ($model->load(Yii::$app->request->post()) && $model->login()) { | ||
return $this->goBack(); | ||
} | ||
return $this->render('login', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
/** | ||
* Logout action. | ||
* | ||
* @return string | ||
*/ | ||
public function actionLogout() | ||
{ | ||
Yii::$app->user->logout(); | ||
|
||
return $this->goHome(); | ||
} | ||
|
||
/** | ||
* Displays contact page. | ||
* | ||
* @return string | ||
*/ | ||
public function actionContact() | ||
{ | ||
$model = new ContactForm(); | ||
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { | ||
Yii::$app->session->setFlash('contactFormSubmitted'); | ||
|
||
return $this->refresh(); | ||
} | ||
return $this->render('contact', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.