• Select, Insert, update and delete data in Cake php 3.8

  • Back
 by 

Steps MVC path

  1. For Controller: src\Controller\NameController.php
  2. For model: src\Model\Table\NameTable.php
  3. For entity: src\Model\Entity\Name.php
  4. For view CTP: src\Template\Name\name.ctp

Code for MVC and its name as Application

So now controller name is ApplicationController.php

namespace App\Controller;
use App\Controller\AppController;

class ApplicationController extends AppController
{
	public function application($application_id=NULL)
		{
			$result_adm_application =  $this->Application->find()->order(['application_id'=>'DESC']);				
			$this->set('adm_application', $result_adm_application);
			if (isset($application_id)) {
				$update_adm_application = $this->Application->get($application_id);
				$this->set('adm_application1', $update_adm_application);
				$data = $this->Application->patchEntity($update_adm_application,$this->request->getData());
            
        } else if (!empty($this->request->getData())) {
            $data = $this->Application->newEntity($this->request->getData());
            
        }
			if($this->request->is(['post','put'])) {
				if($this->Application->save($data)) {
					$this->Flash->success('Data has been saved.');
					return $this->redirect(['action' => 'application']);
				}
			}
		}
public function delete($application_id)
{
$this->request->allowMethod(['post', 'delete']);

$application_id_delete = $this->Application->get($application_id);
if ($this->Application->delete($application_id_delete)) {
$this->Flash->success(__('The application with id: {0} has been deleted.', h($application_id)));
return $this->redirect(['action' => 'application']);
}
}
}

Model : src \Model\Table\ApplicationTable.php

namespace App\Model\Table;

namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class ApplicationTable extends Table
{
    public function initialize(array $config)
    {
		$this->setTable('adm_application');
		$this->setPrimaryKey('application_id');
    }
    public function validationDefault(Validator $validator)
    {
        $validator
	->notEmpty('application_name',('application name'))
            ->notEmpty('description',('application desc'));

        return $validator;
    }
}

For entity: src\Model\Entity\Application.php

<?php 
/*
author pp 11-12-2019
*/

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Application extends Entity
{
protected $_accessible = [
        'application_name' => true,
'description' => true,
    ];
}
?>

For view CTP: src\Template\Name\application.ctp

<?php if(isset($adm_application1)) {
echo $this->Form->create($adm_application1);
}else
{
echo $this->Form->create($adm_application);
}
    echo $this->Form->control('application_name');
    echo $this->Form->control('description', ['rows' => '3']);
    echo $this->Form->button(__('Save Post'));
    echo $this->Form->end();
?>
 <table>
<tr>
<th>application_name</th>
<th>description</th>
</tr><?php
    foreach($adm_application as $values) {
   // pr($values);
     //echo '<pre>';print_r($values);
	echo "<tr><td>".$values->application_name.'</td><td>'. $values->description."</td><td>". $this->Html->link('Edit', ['action' => 'application', $values->application_id])."&nbsp;".$this->Form->postLink('Delete',
                ['action' => 'delete', $values->application_id],
                ['confirm' => 'Are you sure?'])
           ."</td></tr>";    }
?>
</table>			 




Leave your comment

Your email address will not be published. Required fields are marked *