codeigniter 16

Codeigniter 4🔥 [16.- guardar formulario en nuestra base de datos]

En este tutorial vamos a color los datos de nuestro formulario en nuestra base de datos
Codigo en github.com: https://github.com/programadornovato/codeigniter4/commit/ffa39c7f4e3898849ba82477de14f568666f5f6b

//app/Controllers/Home.php
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\UserModel;

class Home extends BaseController
{
	public function __construct(){
		helper('form');
	}
	public function guarda(){
		$userModel=new UserModel($db);
		$request= \Config\Services::request();
		$data=array(
			'name'=>$request->getPostGet('name'),
			'email'=>$request->getPostGet('email'),
		);
		if($userModel->insert($data)===false){
			var_dump($userModel->errors());
		}
		$users=$userModel->findAll();
		$users=array('users'=>$users);
		$estructura=view('estructura/header').view('estructura/body',$users);
		return $estructura;

	}
	public function formulario(){
		$estructura=view('estructura/header').view('estructura/formulario');
		return $estructura;
	}
	public function index()
	{
		$userModel=new UserModel($db);
		//$users=$userModel->find([1,2]);
		//$users=$userModel->findAll();
		//$users=$userModel->where('name','maria')->findAll();
		//$users=$userModel->findAll(2,3);
		//$users=$userModel->withDeleted()->findAll();
		/*
		$data=[
			'name'=>"programador1",
			'email'=>"[email protected]"
		];
		$userModel->insert($data);
		
		$data=[
			'name'=>"programador2",
			'email'=>"[email protected]"
		];
		$userModel->update(10,$data);
		
		$data=[
			'name'=>"yo"
		];
		$userModel->update([2,3],$data);
		
		$userModel->whereIn('id',[4,5,10])
		->set(['name'=>'yo tambien'])
		->update();
		
		$data=[
			'name'=>"programador11",
			'email'=>"[email protected]"
		];
		$userModel->save($data);
		
		$data=[
			'id'=>"12",
			'name'=>"programador yo tambien",
			'email'=>"[email protected]"
		];
		$userModel->save($data);
		*/
		//$userModel->delete([2,4,5]);
		//$userModel->where('id',10)->delete();
		//$userModel->purgeDeleted();
		/*
		$data=[
			'name'=>"programadorvalido nuevo 2",
			'email'=>"[email protected]"
		];
		if($userModel->save($data)===false){
			var_dump($userModel->errors());
		}
		*/
		/*
		$users=$userModel->asArray()->where('name','programador11')
		->orderBy('id','asc')
		->findAll();
		var_dump($users);
		*/
		$users=$userModel->findAll();
		
		$users=array('users'=>$users);
		$estructura=view('estructura/header').view('estructura/body',$users);
		return $estructura;
		
	}

	//--------------------------------------------------------------------

}







//app/Models/UserModel.php
<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
        protected $table      = 'users';
        protected $primaryKey = 'id';

        protected $returnType = 'array';
        protected $useSoftDeletes = true;

        protected $allowedFields = ['name', 'email'];

        protected $useTimestamps = false;
        protected $createdField  = 'created_at';
        protected $updatedField  = 'updated_at';

        protected $validationRules    = [
                'name' => 'required|alpha_numeric_space|min_length[3]',
                'email' =>'required|valid_email|is_unique[users.email]'
        ];
        protected $validationMessages = [
                'email' =>[
                        'is_unique'=>'Lo sentimos. Tu correo ya esta siendo usado por otro usuario'
                ]
        ];
        protected $skipValidation     = false;
        //protected $beforeInsert=['agregaAlgoName'];
        //protected $beforeUpdate=['agregaAlgoName'];
        protected function agregaAlgoName(array $data){
                $data['data']['name']=$data['data']['name']." algo";
                return $data;
        }
}






//app/Views/estructura/formulario.php 
<?php
echo form_open('/home/guarda');
echo form_label('Nombre','name');
echo form_input(array('name'=>'name','placeholder'=>'Nombre'));
echo "<br>";
echo form_label('Email','email');
echo form_input(array('name'=>'email','placeholder'=>'Email'));
echo "<br>";
echo form_submit('guarda','Guardar');

echo form_close();
?>


Guia oficial codeigniter 4: https://codeigniter4.github.io/userguide/models/model.html

Codeigniter 4? [16.- guardar formulario en nuestra base de datos]

Codigo en github.com: https://github.com/programadornovato/codeigniter4/commit/806620237c14a60ccb039eed10ebbbc13a22b749 #Codeigniter4 #php7 #backend #Codeigniter #php .
? Esta lista de reproducción: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTFN5TDeOP-wP_hU0_9VoWg
.
Codigos en gdrive: https://drive.google.com/file/d/1awbG6jI40IO0N68fgjLUwNFpsTC3wIHj/view?usp=sharing .
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true .
? Facebook: https://facebook.com/ProgramadorNovatoOficial
? Twitter: https://twitter.com/programadornova
? Linkedin: https://www.linkedin.com/in/programadornovato/
? Instagram: https://www.instagram.com/programadornovato/

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

En Programador Novato vivimos de los anuncios, hemos detectado que está utilizando extensiones para bloquear anuncios. Ayudanos deshabilitando tu bloqueador de anuncios. :)