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

Hi everyone !

I have a controller, that checks if a record already exists in the database. If it does, it performs an update instead of an insertion.

But I want to take full advantages of all the models' features. So I'm trying to do the same thing, but with a function called using $beforeInsert.

My problem is that I don't know how to cancel the insertion, as CI expects an array... Someone has any clue on how I can do this ?

Here is the code I have in my model:

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()" (could have been "update()" too)
      $this->save($data['data']); // THIS IS WORKING
      // And we stop the insertion
      return FALSE// THIS IS NOT WORKING -> ARRAY EXPECTED -> WHAT CAN I DO ?
    }
  


Thank you in advance for your help !
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB