PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to pass value of parent id in child controller in cakephp3

 October 21, 2022     cakephp-3.0, has-many     No comments   

Issue

I have just started learning cakephp3, so please excuse me for anything wrong. So I have one table called Projects and Events, so there relation is like one project can have many events.

When the user is adding a new project, he will be redirected to 'add' event in Controller 'events '.

testapp/projects/add --> testapp/events/add

The code in default add.ctp has a dropdown of projects but i want it to be the normal text field with the value of just created project instead of dropdown.

Please tell how can I achieve this?

Thanks in advance


Solution

somewhere in your ProjectsController you shoud have something like

redirect(['action' => 'add', 'controller' => 'Events'])

and you should have a variable named $project storing a ProjectEntity

just pass the project id to the redirect action

redirect(['action' => 'add', 'controller' => 'Events', $project->id])

then in your EventsController you'll read the value and store it to the Event entity this way

function add($project_id)
{
    // Do something
    $event = $this->Events->newEntity();
    $event->project_id = $project_id; 
    // do something else
}


Answered By - arilia
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing