0
votes

I have this model :

School hasMany Teachers

I have a view action/page on my School controller, where I need to put a "add new teacher for this school" link. This link must lead to the Teacher add page, and the new teacher must reference the school from the previous page.

I am doing this by adding a query string on my link, like :

<a href="/teachers/add?school_id=4">Add new teacher for this school</a>

Then in my TeachersController I pass this value to my view (teacher::add.ctp) as $school_id and create a hidden input in the add.ctp like :

$this->hidden('school_id', ['value' => $school_id])

So when I submit the teacher add form, its school_id field is set correctly.

Is there a better way to do this ? I am not really happy with my solution and the query string trick (one could change this in the address bar...).

Thanks

1

1 Answers

1
votes

I guess you'll have to pass the information to the action in some way.

Instead of a link you can use a form (cake FormHelper has a formlink() method for this) and pass school_id via POST

Anyway even if you use POST data there are still many ways a user could modify the data.

The problem I see here is that you use an hidden field. I think it's redundant because when you send your data to /teachers/add?school_id=4you already have this information in your query data

so you can do

$teacher =  $this->Teachers->newEntity()
$teacher = $this->Teachers->patchEntity($teacher , $this->request->data);
$teacher->school_id = $this->request->query('teacher_id');