How can I pass parameters from one controller to another

2 views
Skip to first unread message

Oscar Canseco

unread,
Feb 22, 2008, 2:37:31 PM2/22/08
to Akelos PHP Framework
I have a model called organization and this has many producers, I want
to pass the id of organization to the organization_id on producer when
I'm adding a new producer to the current organization?

How can I do this, please help (I'm a newbie of course)

My add function look like this

function add()
{
if(!empty($this->params['producer'])){
$this->Producer->setAttributes($this->params['producer']);

$this->Producer->organization_id = $this->organization-
>get('id'); // this is obviously not working what can I do

if ($this->Request->isPost() && $this->Producer->save()){
$this->flash['notice'] = $this->t('Producer was
successfully created.');
$this->redirectTo(array('action' => 'show', 'id' =>
$this->producer->getId()));
}
}
}

Do I need to add anything to the producer model???

Bermi Ferrer

unread,
Feb 22, 2008, 2:46:53 PM2/22/08
to akelos-f...@googlegroups.com
You should have created an association between models like:

class Producer extends ActiveRecord{
var $belons_to = 'organization';
}

class Organization extends ActiveRecord{
var $has_many = 'producers';
}

Then in your controller you can just do.

class ProducerController extends ApplicationController{}
var $moels = 'Producer,Organization';

function add()
{
if(!empty($this->params['producer'])){
$this->Producer->setAttributes($this->params['producer']);

if ($this->Request->isPost() && $this->Producer->save() &&
$this->Producer->organization->set($this-
>Organization->find($this->params['organization_id'])) ){


$this->flash['notice'] = $this->t('Producer was
successfully created.');
$this->redirectTo(array('action' => 'show', 'id' =>
$this->producer->getId()));
}
}
}

Bermi

Reply all
Reply to author
Forward
0 new messages