Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
$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();
- 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...