CodeIgniter Forums
Good example of CRUD - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Good example of CRUD (/showthread.php?tid=87417)



Good example of CRUD - joho - 04-18-2023

Would this considered a good example for CRUD in CI4?

https://makitweb.com/crud-create-read-update-delete-in-a-codeigniter-4/

What I'm after, at the moment, is the differences between Create and Update. I have creating (new) entries working, I have view (existing) entries working, but for some reason I can't seem to nail down Edit/Update. I'm trying to use the same function ("edit") in my controller for Create and Edit. If it's a GET request, and the record ID is set, I assume we're editing, so I read the record from DB. If it's a POST request, I validate (and possibly save/update). But I can't seem to populate the form properly in the controller.
Creating and saving is not a problem.


RE: Good example of CRUD - captain-sensible - 04-18-2023

this is how i update in a model , to do with updating a blog article . If logged in then i can access simple menu, to pick blog i want to edit. Current data of blog is output to a form, to do the editing .

Form goes to a controller and gets the info i need. Final bit is where model does the updating
allowed field of model

Code:
class BlogModel extends Model

{

protected $table      = 'blog';
protected $primaryKey = 'Id';
protected $allowedFields = ['title','article','image','slug','date','context'];
protected $limit;
protected $offset;
protected $Id;
protected $title;
protected $article;
protected $slug;


Code:
public function amendBlog($blogId,$title,$article,$slug)
    
    
     {
    
     $this->Id= $blogId;
     $this->title=  $title;
     $this->article =  $article;
     $this->slug=$slug;
     $data =[
     'title'=>$this->title,
     'article'=>$this->article,
     'slug'=>$this->slug
     ];
   $logic=   $this->update($this->Id,$data);
   return $logic;
      
     }

but to be honest i don't like things being done for me, if i write my own code, at least i know how validating is done and whats involved


RE: Good example of CRUD - joho - 04-18-2023

Thank you. What I can't quite wrap my head around in the way I've got it setup now, is why the code behaves differently when I'm in edit mode vs create mode. It should be just a case of, in edit mode, to populate the initial post data, and then pretend it's create (which then turns to update upon save because there's a record ID present).

When I initially get the edit view, the field(s) are populated, and using the old('foo', $foo) form construct, but upon submitting, $foo is unknown. And if it works upon initial edit, why doesn't it work after? :-)