Skip to content

Conversation

ghost
Copy link

@ghost ghost commented Jan 27, 2020

First I would like to thank @challgren for their help. Thanks to him I was able to understand this plugin and because of that I suggest the following changes to make it more didactic.

CHANGES TO README.MD

Usage
See a practical step-by-step example below.

  1. In this example we will use the 'sti_cooks' table with the following structure:

        $this->table('sti_cooks')
            ->addColumn('name', 'string', [
                'default' => null,
                'limit' => 64,
                'null' => false,
            ])
            ->addColumn('type', 'string', [
                'default' => null,
                'limit' => 32,
                'null' => false,
            ])
            ->addColumn('age', 'integer', [
                'default' => null,
                'limit' => 11,
                'null' => true,
            ])
            ->create();

  1. Note that we created three calls to this table, each independent of each other and all referring to the same 'sti_cooks' table.
setTable('sti_cooks');         $this->addBehavior('Muffin/Sti.Sti', [             'typeMap' => [                 'chef' => 'App\Model\Entity\Chef',                 'baker' => 'App\Model\Entity\Baker',                 'assistant_chef' => 'App\Model\Entity\AssistantChef',             ],         ]); // Optionally, set the default type. If none is defined, the // first one (i.e. `chef`) will be used. // $this->setEntityClass('AssistantChef');     }     public function validationBaker(Validator $validator)     {         if (method_exists($validator, 'notEmptyString')) {             $validator->notEmptyString('name', 'baker');         } else {             $validator->notEmpty('name', 'baker');         }     } } 3. Next, create the following classes (note that you don't need to create the Cooks class, as the Chef class will be used). The entity that was previously defined to be the 'default' one will need to use the StiAwareTrait :: type) {             return Inflector::humanize($this->type);         }         return null;     } } Optionally, you can create classes for your tables that extend the parent table to encapsulate business logic:  [             'type' => 'chef'         ]     ];     public function index()     {         $chefs = $this->paginate($this->Cooks);         $this->set(compact('chefs'));     }     public function view($id = null)     {         $chef = $this->Cooks->get($id, [             'contain' => [],         ]);         $this->set('chef', $chef);     }     public function add()     {         $chef = $this->Cooks->newEntity(['type' => 'chef']);         if ($this->request->is('post')) {             $chef = $this->Cooks->patchEntity($chef, $this->request->getData());             if ($this->Cooks->save($chef)) {                 $this->Flash->success(__('The chef has been saved.'));                 return $this->redirect(['action' => 'index']);             }             $this->Flash->error(__('The chef could not be saved. Please, try again.'));         }         $this->set(compact('chef'));     }  [             'type' => 'baker'         ]     ];    public function index()     {         $bakers = $this->paginate($this->Cooks);         $this->set(compact('bakers'));     }     public function view($id = null)     {         $baker = $this->Cooks->get($id, [             'contain' => [],         ]);         $this->set('baker', $baker);     }     public function add()     {         $baker = $this->Cooks->newEntity(['type' => 'baker']);         if ($this->request->is('post')) {             $baker = $this->Cooks->patchEntity($baker, $this->request->getData());             if ($this->Cooks->save($baker)) {                 $this->Flash->success(__('The baker has been saved.'));                 return $this->redirect(['action' => 'index']);             }             $this->Flash->error(__('The baker could not be saved. Please, try again.'));         }         $this->set(compact('baker'));     }  [             'type' => 'assistant_chef'         ]     ];     public function index()     {         $assistantChefs = $this->paginate($this->Cooks);         $this->set(compact('assistantChefs'));     }     public function view($id = null)     {         $assistantChef = $this->Cooks->get($id, [             'contain' => [],         ]);         $this->set('assistantChef', $assistantChef);     }     public function add()     {         $assistantChef = $this->Cooks->newEntity(['type' => 'assistant_chef']);         if ($this->request->is('post')) {             $assistantChef = $this->Cooks->patchEntity($assistantChef, $this->request->getData());             if ($this->Cooks->save($assistantChef)) {                 $this->Flash->success(__('The assistant chef has been saved.'));                 return $this->redirect(['action' => 'index']);             }             $this->Flash->error(__('The assistant chef could not be saved. Please, try again.'));         }         $this->set(compact('assistantChef'));     } New entities The behavior ………. follow as it is in the original...

ADmad and others added 4 commits January 16, 2020 23:21
`$this->table()` is deprecated so updated it to `$this->setTable()`
[3.6] Updating docs to update deprecated example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants