Welcome Guest, Not a member yet? Register   Sign In
Updating Model with a unique field?
#1

(This post was last modified: 05-04-2019, 05:47 PM by MeltedRolo.)

Hi guys

Just working on an update in the model controller and have hit a snag.

I have setup one of the fields as unique, when I try an update, it always fails, unless that particular field has changed and is still unique.

Is there a way to temporarily turn off the unique check based on if it has changed or not? because I may not want to change that field.

Thanks
Reply
#2

I don’t think there’s a way to do a temporary change. You’d have to share some code if you want more specifics, but most likely the framework is reflecting what it believes is happening in the database and is preventing a fatal error.
Reply
#3

(This post was last modified: 05-05-2019, 10:12 AM by MeltedRolo.)

Hi

Thanks for the reply.

I got it sorted, just checked if the field had changed in the controller, and updated the fields accordingly.


PHP Code:
<?php namespace App\Controllers;

use 
App\Models\BlogModel;
use 
CodeIgniter\Controller;

helper('form');

class 
BlogController extends BaseController {

 
   public function index() {

 
       $page_data = array();
 
       $page_data['page_title'] = 'Blog Articlies';

 
       $model = new BlogModel();

 
       $page_data['blogs'] = esc($model->getBlogs());

 
       if (!isset($page_data['blogs']['0'])) {

 
           throw new \CodeIgniter\Exceptions\PageNotFoundException('There are currently no blog articles to view.');
 
       }

 
       $page_data['title'] = 'Blog Articles';

 
       echo view('header'$page_data);
 
       echo view('blog/overview'$page_data);
 
       echo view('footer'$page_data);
 
   }

 
   public function view($blog_id null) {

 
       $page_data = array();

 
       $model = new BlogModel();

 
       $page_data['blogs'] = esc($model->getBlogs($blog_id));

 
       if (!is_array($page_data['blogs'])) {

 
           throw new \CodeIgniter\Exceptions\PageNotFoundException('This Blog article does not exist.');
 
       }

 
       $page_data['page_title'] = 'Blog Article #' $page_data['blogs']['blog_id'];
 
       $page_data['title'] = $page_data['blogs']['title'];

 
       echo view('header'$page_data);
 
       echo view('blog/view'$page_data);
 
       echo view('footer'$page_data);
 
   }

 
   public function create() {

 
       $page_data = array();
 
       $page_data['page_title'] = 'Create New Blog Article';

 
       $model = new BlogModel();

 
       if (!$this->request->getVar(esc('submit'))) {

 
           $page_data['title'] = 'Create a new Blog Article.';

 
           echo view('header'$page_data);
 
           echo view('blog/create'$page_data);
 
           echo view('footer'$page_data);
 
       } else {

 
           $result $model->saveBlog(['title' => $this->request->getVar(esc('title')),
 
                                       'intro' => $this->request->getVar(esc('intro')),
 
                                       'body'  => $this->request->getVar(esc('body'))
 
                                       ]);
 
           if($result === false) {

 
               $page_data['title'] = 'Create a new Blog Article.';
 
               $page_data['errors'] = $model->errors();

 
               echo view('header'$page_data);
 
               echo view('blog/create'$page_data);
 
               echo view('footer'$page_data);
 
           } else {

 
               $page_data['inserted_id'] = $model->insertID();
 
               $page_data['redirect'] = array('time' => 5'uri' => $this->request->uri);

 
               echo view('header'$page_data);
 
               echo view('blog/success'$page_data);
 
               echo view('footer'$page_data);
 
           }
 
       }
 
   }

 
   public function update($blog_id null) {

 
       $page_data = array();
 
       $page_data['page_title'] = 'Update Blog Article.';

 
       $model = new BlogModel();

 
       $blog_data $model->getBlogs($blod_id $this->request->uri->getSegment(3));
 
       //echo date('Y-m-d H:i:s');
 
       if($blog_data === null) {

 
           throw new \CodeIgniter\Exceptions\PageNotFoundException('This Blog article does not exist.');
 
       } else {
 
           if (!$this->request->getVar(esc('submit'))) {

 
               $page_data['title'] = 'Update Blog Article.';
 
               $page_data['blog_data'] = $blog_data;
 
               $page_data['errors'] = $model->errors();

 
               echo view('header'$page_data);
 
               echo view('blog/update'$page_data);
 
               echo view('footer'$page_data);
 
           } else {

 
               if($blog_data['title'] === $this->request->getVar(esc('title'))) {

 
                   $array = ['intro' => $this->request->getVar(esc('intro')),
 
                             'body'  => $this->request->getVar(esc('body'))
 
                            ]; 
 
               } else {

 
                   $array = ['title' => $this->request->getVar(esc('title')),
 
                             'intro' => $this->request->getVar(esc('intro')),
 
                             'body'  => $this->request->getVar(esc('body'))
 
                            ]; 
 
               }
 
               
                $result 
$model->update($blog_data['blog_id'], $array);

 
               if($result === false) {

 
                   $page_data['title'] = 'Create a new Blog Article.';
 
                   $page_data['blog_data'] = $array;
 
                   $page_data['blog_data']['blog_id'] = $blog_data['blog_id'];
 
                   $page_data['errors'] = $model->errors();

 
                       echo view('header'$page_data);
 
                       echo view('blog/update'$page_data);
 
                       echo view('footer'$page_data);
 
               }
 
           }
 
       }
 
   }


The code in question is in the update method.

I guess I should really be checking all fields for any change, and only updating if and when a change has been detected.

It's amazing that a good night's sleep, can cure almost any coding problem Smile.

But I would be interested in critics of the above code so far.

Thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB