Welcome Guest, Not a member yet? Register   Sign In
Cancel $model->save($data) operation using $beforeInsert
#3

Hello,

Thank you for your answer.

I saw this part of the code. But I couldn't find a way to make the trigger stop the rest of the code, without touching/extending core files.


So far, here is the "dirty" solution I found... (for those would might read this post later)


PHP Code:
  protected $beforeInsert = ['uniqueTokenPerUser'];

  protected function uniqueTokenPerUser(array $data)
  {
    if (! isset($data['data']['user']) ) { return $data; }

    // We check if a row already exists
    $currentToken $this->where('user',$data['data']['user'])->first();
    // If the row exists
    if( $currentToken !== FALSE ) {
      // We set the ID
      $data['data']['id'] = $currentToken['id'];
      // And perform an "update" using "save()"
      $this->save($data['data']);
      // And we stop the insertion with a "hard" redirection
      header('location: '.base_url('app/profile')); // Using "header" because "route()" doesn't want to work :-)
      exit; // Hard break
    }
  



I don't really like it, because it is now complicated to return the errors to the controller and the view. I am probably going to manage this in the controller (as I was doing originally). That is too much work for a simple operation. I thought maybe someone had a "miracle" solution :-)

Thank you anyway ;-)
Reply


Messages In This Thread
RE: Cancel $model->save($data) operation using $beforeInsert - by littlej - 01-06-2020, 09:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB