CRUD operation in CakePGP 3

behavior

#1

Hello. I am new in CakePHP 3. Recently I cake bake a table named CmleLanguages. The add, edit, index as well as view cake templates are generated in src. But I do not understand the
codes of CmleLanguages controller. The code segment is below:


class CmleLanguagesController extends AppController
{

    public function index()
    {
        $cmleLanguages = $this->paginate($this->CmleLanguages);

        $this->set(compact('cmleLanguages'));
    }

    public function view($id = null)
    {
        $cmleLanguage = $this->CmleLanguages->get($id, [
            'contain' => []
        ]);

        $this->set('cmleLanguage', $cmleLanguage);
    }

    public function add()
    {
        $cmleLanguage = $this->CmleLanguages->newEntity();
        if ($this->request->is('post')) {
            $cmleLanguage = $this->CmleLanguages->patchEntity($cmleLanguage, $this->request->getData());
            if ($this->CmleLanguages->save($cmleLanguage)) {
                $this->Flash->success(__('The cmle language has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The cmle language could not be saved. Please, try again.'));
        }
        $this->set(compact('cmleLanguage'));
    }

    public function edit($id = null)
    {
        $cmleLanguage = $this->CmleLanguages->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $cmleLanguage = $this->CmleLanguages->patchEntity($cmleLanguage, $this->request->getData());
            if ($this->CmleLanguages->save($cmleLanguage)) {
                $this->Flash->success(__('The cmle language has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The cmle language could not be saved. Please, try again.'));
        }
        $this->set(compact('cmleLanguage'));
    }

    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $cmleLanguage = $this->CmleLanguages->get($id);
        if ($this->CmleLanguages->delete($cmleLanguage)) {
            $this->Flash->success(__('The cmle language has been deleted.'));
        } else {
            $this->Flash->error(__('The cmle language could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}

Now I am wanting to understand line by line code so that I can realize how these codes are working.

Is there any resource about it?

Thanks.


#2

The functions it uses are all well documented in the cookbook. If there are specifics you want to ask about, that’s much more appropriate than asking for a line by line breakdown.


#3

Thanks for sharing .