Yii - using afterDelete() to update data in another table - php

I am using Yii afterdelete() to update the related data which is deleted in another table. Here is my code in the controller:
Controller Action
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
Model function
protected function afterDelete()
{
parent::afterDelete();
$show_model = new Show();
$show_model = Show::model()->findAll('tbl_season_id='.$this->id);
$show_model->updateAll('tbl_season_id = NULL, on_season=0');
}

As #Gregor said, a good use of active record relations would make the job much easier.
So, in Show model you would have something like:
public function relations()
{
return array(
'season' => array(self::BELONGS_TO, 'Season', 'tbl_season_id'),
);
}
While in Season model you would have something like:
public function relations()
{
return array(
'shows' => array(self::HAS_MANY, 'Show', 'tbl_show_id'),
);
}
Having relations defined, will give you the ability to do this:
public function afterDelete()
{
parent::afterDelete();
$season_shows = Season::model()->findByID($id)->shows; //using the shows relation
foreach($season_shows as $season_show) do
{
$season_show->setAttributes('tbl_season_id => NULL, on_season => 0');
$season_show->save();
}
}
Hummm, but if you noticed that second line in the afterDelete which calls findByID($id) but we are inside the afterDelete and the record is actually dead (deleted)!!
To fix this you can grab the id just before the model is deleted using a variable & abeforeDelete
//at the begining of your model class
$private = $cached_season_id;
...
//then somewhere at the end
...
public function beforeDelete()
{
$this->cached_tbl_season_id = $this->id;
return parent::beforeDelete();
}
Now if you change the id in the afterDelete to $this->cached_season_id .. it should work.
Well, this solution is based on this yii-fourm-topic and i am not quite sure if it's going to work as it is!! So, give it a try & let us know what happens?

That looks an awful lot like a HAS_MANY relationship from Season to Show, so you might want to use relations in the future to get related records. There is a pretty good documentation for that in the yii-guide: http://www.yiiframework.com/doc/guide/1.1/en/database.arr
It also seems to me, that you have a jQuery-background. You are calling updateAll on an array(which is returned by the findAll-function).
A proper updateAll-call might look like this:
Show::model()->updateAll(array("tbl_season_id"=>null, "on_season"=>0), "tbl_season_id = $this->id")
This would probably be even better with some kind of constraint but since that is imo a matter of taste, I'll leave it at that.

Related

pass data to edit from the controller to the view in a many to many relationship

Hi everyone i have a many-to-many relationship between the turnos table and the dias table like this:
Currently, I'm doing the CRUD of the turnos table and for each turnos I have to assign many dias, I did it with the attach method.
Now the issue is in the edit method... how am I gonna get the assigned dias that is related to that turno so I can pass it to the view and the user can edit it?
If someone knows it please help me, I would appreciate it very much
//Dias Model
public function turnos()
{
return $this->belongsToMany(Turno::class);
}
//Turnos Model
public function dias()
{
return $this->belongsToMany(Dia::class);
}
// Controller
public function edit(Turno $turno)
{
// $dias = ??
return Inertia::render('Turnos/Editar', [
'turno' => $turno,
'dias' => ??
]);
}
The edit view Should looks like this:
You can load the relation with the load() method and just return the $turno variable that will contain the "turno" and the "dias".
public function edit(Turno $turno) {
$turno->load('dias');
return Inertia::render('Turnos/Editar', [
'turno' => $turno
]);
}
On the client side you can use v-model to fill your inputs.

Laravel timestamps keep track of who has done certain action (like created_at & created_by, updated_at & updated_by)

In my application I want to keep track of who has performed certain operations on different models in my application.
Default Laravel model with timestamps automatically updates fields like created_at and updated_at. I can modify this behavior to set the created_by field automatically by calling the static::updating() function as mentioned in this answer: https://stackoverflow.com/a/64241347/4112883 . This works very well. Additionally, I came across this package (https://github.com/WildsideUK/Laravel-Userstamps), but that is limited to only created, updated, and deleted.
For my Post model, I have more timestamps: created_at, updated_at, completed_at, checked_at, and published_at. When a user ends the post, it must be verified by that user's manager. If all is well, some logic will publish the message, but if not, the manager can create one or more actions for the user to complete the message, which will undo the finishing attributes. An action is created with the following timestamps: created, updated, and completed (null). When the user completes an action, the actions.finished_at and actions.finished_by fields are set.
Now comes the challenge. For each custom timestamp, I want to set the relationship and three functions to handle certain states of the timestamp: set, undo and check for isset:
class Post extends Model
{
//…
public function finishedBy() //relationship belongsTo User::class
{
return $this->belongsTo(User::class, 'finished_by');
}
public function finish() { //function to finish post (SET)
$this->update([
'finished_by' => auth()->id(),
'finished_at' => now(),
]);
}
public function undoFinish() { //function to undo finishing (UNSET)
$this->update([
'finished_at' => null,
'finished_by' => null,
]);
}
public function isFinished() { //function to check if is finished (ISSET)
return !empty($this->finished_by) && !empty($this->finished_at);
}
//…
All four functions must be repeated for ‘checked’ and ‘published’ in the Post model, and for the ‘finished’ attribute in Action model, leading to a lot of almost-duplicate code. (Maybe in the future I want to repeat this logic in other models.)
Is there a possibility to make this more elegant with a Trait or something?
E.g. create something like an protected array $timestamps_with_user by which the application automatically adds the relationship and the three functions?
protected $timestamps_with_users = [
'finish', 'check', 'publish'
];
// foreach in a trait?? Need your help here :D
foreach($timestamps_with_users as $perform) {
public function $perform() { … } //$post->finish()
public function $perform.edBy() :User { … } //$post->finishedBy()
public function undo.$perform() { … } //$post->undoFinish()
public function is.$perform.ed() { … } //$post->isFinished()
}
Thanks in advance and looking forward to your answers.
Just create a new trait and create functions that works with any timestamp:
<?php
namespace App\Traits;
trait CustomTimestamps {
public function perform(string $action)
{
$this->update([
$action . 'ed_by' => auth()->id(),
$action . 'ed_at' => now(),
]);
}
public function undo(string $action)
{
$this->update([
$action . 'ed_by' => null,
$action . 'ed_at' => null,
]);
}
public function check(string $action)
{
$at = $action . 'ed_at';
$by = $action . 'ed_by';
return !empty($this->{$by}) && !empty($this->{$at});
}
}

How to delegate "delete" operation to another Model in Cakephp4?

I have 2 Models (SuperRubriques and CustomRubriques) using the same table rubriques in DB.
When I delete from SuperRubriques, I would like to delegate the delete to CustomRubriques (as CustomRubriques has a hasOne association with extended_rubriques that SuperRubriques doesn't know).
For info, the rubriques table in DB has the field model containing 'CustomRubriques' (i.e. the Model with which it has been saved).
I've tried to do it in SuperRubriquesTable::beforeDete() :
// In SuperRubriquesTable.php
public function beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)
{
$table = TableRegistry::getTableLocator()->get($entity->model); // $entity->model contains 'CustomRubriques'
$rubriqueEntity = $table->get($entity->id);
return $table->delete($rubriqueEntity);
}
However $table->delete($rubriqueEntity) is true (when I debug) but the record is not deleted in DB, I don't know why?
I've fix the issue :
Instead of deleguating inside SuperRubriquesTable::beforeDelete(), I've done the job in SuperRubriquesController::delete() :
// In SuperRubriquesController
public function delete($id)
{
$this->request->allowMethod(['post', 'delete']);
$rubrique = $this->SuperRubriques->get($id);
$rubriqueModel = $rubrique->model;// contains 'CustomRubriques'
$this->loadModel($rubriqueModel);
$rubriqueEntity = $this->$rubriqueModel->get($id);
if ($this->$rubriqueModel->delete($rubriqueEntity)) {
return $this->redirect(['action' => 'index']);
}
}

Handle form submission Laravel 5

I just start learning Laravel 5, and I want to know what the proper way to handle submitted forms. I found many tutorials where we create two separate actions, where first render form, and the second actually handle form. I am came from Symfony2, where we create a single action for both, render and handle submitted form, so I want to know I need to create two separate actions because thats Laravel-way, or I can place all logic into single action, I do this like the folowing, but I dont like code what I get:
public function create(Request $request)
{
if (Input::get('title') !== null) {
$v = Validator::make($request->all(), [
'title' => 'required|unique:posts',
'content' => 'required',
]);
if ($v->fails()) {
return redirect()->back()->withErrors($v->errors());
}
$post = new Post(Input::all());
if ($post->save()) {
return redirect('posts');
}
}
return view('add_post');
}
So can somebody give me advice how I need do this properly? Thanks!
One of the most important reason to create two actions is to avoid duplicate form submissions . You can read more about Post/Redirect/Get pattern.
Another important reason is the way you keep the code cleaner. Take a look at this first change:
public function showForm(){
return view('add_post');
}
public function create(Request $request)
{
$v = Validator::make($request->all(), [
'title' => 'required|unique:posts',
'content' => 'required',
]);
if ($v->fails()) {
return redirect()->back()->withErrors($v->errors());
}
$post = new Post(Input::all());
if ($post->save()) {
return redirect('posts');
}
return redirect()->route('show_form')->withMessage();
}
The first thing that you can notice is that create() function is not rendering any view, it is used to manage the creation logic (as the name itself suggests). That is OK if you plan to stay in low-profile, but what happens when you do need to add some others validations or even better, re-utilize the code in other controllers. For example, your form is a help tool to publish a comment and you want to allow only "authors-ranked" users to comment. This consideration can be manage more easily separating the code in specific actions instead making an if-if-if-if spaghetti. Again...
public function showForm(){
return view('add_post');
}
public function create(PublishPostRequest $request)
{
$post = new Post($request->all());
$post->save()
return redirect('posts');
}
Take a look on how PublishPostRequest request takes place in the appropriated function. Finally, in order to get the best of Laravel 5 you could create a request class to keep all the code related with validation and authorization inside it:
class PublishPostRequest extends Request{
public function rules(){
return [
'title' => 'required|unique:posts',
'content' => 'required',
]
}
public function authorize(){
$allowedToPost = \Auth::user()->isAuthor();
// if the user is not an author he can't post
return $allowedToPost;
}
}
One nice thing about custom request class class is that once is injected in the controller via function parameter, it runs automatically, so you do not need to worry about $v->fails()

How to filter records with RESTful models and controllers in Yii2

I'm creating a RESTful API with Yii2 and have successfully setup a model named Contacts by following the Quick Start Tutorial*. I love how records can be created, listed, updated and deleted without creating any actions.
However I can't see how to filter results. I would like to only return contacts where contact.user_id is equal to 1 (for example) as it currently will reply with all records. Is this possible without creating the actions?
I am unsure also how I can limit results. From what I've read I feel it should append the URI with ?limit=5.
http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html
You should return a dataprovider instead of a set of objects, that supports pagination for you.
Perhaps this approach will be a bit more useful:
public function actionIndex()
{
return new \yii\data\ActiveDataProvider([
'query' => Contact::find()->where(['user_id' => \Yii::$app->user-id]),
]);
}
You could also leave the index action intact, but provide the preset action with a prepareDataProvider-callback:
public function actions()
{
$actions = parent::actions();
$actions['index']['prepareDataProvider'] = function($action)
{
return new \yii\data\ActiveDataProvider([
'query' => Contact::find()->where(['user_id' => \Yii::$app->user-id]),
]);
};
return $actions;
}
Hope that helps.
I have had to override the index method despite not wanting to. My solution looks like this:
public function actions()
{
$actions = parent::actions();
unset($actions['index']);
return $actions;
}
public function actionIndex()
{
return Contact::findAll(['user_id' => \Yii::$app()->user-id]);
}
I guess this solution means I need to write my own pagination code however which is something else I was hoping to avoid.

Categories