Ok so I figured out what the issue was.
When posting changes the form was not going to "/network/add_network/<
$nId>" it was only going to "/network/add_network/" so this is why it
would lose the $nId parameter.
A couple of factors for this issue (main one below):
1) when I set the variable from controller to view it was losing the
value. (I figured it out to be a Variable conflict, so I renamed
$networkId to $netId)
Edited Code that is working with View:
[code]
public function add_network($nId = 0)
{
if($nId){
$net_info = $this->Network->find('first', array('conditions'
=> array('id' => $nId)));
//Should changed to $this->set();
self::set('netId', $nId); //This variable is used on view
to set the form to post to correct "action with ID"
self::set('network', $net_info);
self::set('page_title', "View | Edit Network".
$net_info['Network']['network_name']." ID: ".$nId);
} else {
self::set('netId', null);
self::set('network', null);
self::set('page_title', "Add Network");
}
//Clear "Flash"
$alert = null;
if ($this->data['network']) {
if ($this->data['network']['network_name']) {
$data['network_name'] = addslashes($this-
>data['network']['network_name']);
$data['sub_id'] = addslashes($this->data['network']
['sub_id']);
$data['active'] = 1;
//Save form data to Networks database.
//If we are editing a network, then make sure to save
data to record ID ($nId)
$mySaveId = ($nId)?$nId:0;
//Start Save to current record
$this->Network->id = $mySaveId;
$this->Network->save($data);
//Get current Record (double check)
$finalId = $this->Network->id;
//Compare to determine correct message to send
to user.
if ($finalId == $mySaveId) {
$alert = "Changes to network: ".
$data['network_name']." were saved.";
} else {
$alert = "New network: ".$data['network_name']."
was added.";
}
}
}
//Handles UI controls (navigation)
switch($this->data['window']['flag']){
case "networks":
self::redirect("/admin/manage_network");
break;
} // switch
//Sets View configurations for UI
self::set('logo', $this->Session->read('logo'));
self::set('wbtitle', $this->Session->read('wbtitle'));
self::set('alert', $alert);
//This is where the bug was, not sure if it needs to be
removed. But to fix bug
//renamed $networkId on view to $netId for now.
self::set('networkId', isset($networkId)?$networkId:null);
}
[/code]
On Feb 11, 11:01 am, chris varick <[email protected]> wrote:
> Stephen,
>
> STILL NOT WORKING... :(
>
> Thanks for the reply and suggestion. However the method you suggested
> gives me different results.
> This is what I ended up using:
> [code]
>
> $mySaveId = $this->Network->id = $nId;
> $networkId = $this->Network->save($this->data);
>
> [/code]
>
> Nothing is "saved" (editing record) or even "inserted" (if it is a new
> record) I am really not sure what is happening. LIke I was saying it
> feels like I am losing the Variable value $nId. $nId is being passed
> by assessing the method "/networks/add_network/<nId>"
> So I am really confused! :P Thank you for the corrected method though
> of saving record via primary key with Model->id=$val I didn't realize
> that was the documented way of doing things.
>
> On Feb 11, 2:46 am, Stephen Speakman <[email protected]>
> wrote:
>
>
>
>
>
>
>
> > It's possible your $_SESSION['nId'] is actually overwriting the
> > parameter being passed to the controller with 0?
> > Assuming you're accessing the method like such: /networks/add_network/<nID>
>
> > If you want to avoid creating a new record try:
>
> > $this->Network->id = $nId;
> > $this->Network->save($this->data);
>
> > Kind Regards
> > Stephen
>
> > On 11/02/2012 08:44, chris varick wrote:
>
> > > I am hoping someone can help me out here. I am pulling my hair out.
> > > For some reason when I go to save "edited" information, form a form,
> > > it is creating an entire new record in the database instead of just
> > > updating the current record at hand.
>
> > > I am using variable $nId to reference the record id on _save(); For
> > > some reason it is setting the $nId to 0. I do not know what is
> > > happening. Any suggestions?
>
> > > Take a look at the line that has: $mySaveId = ($nId)?$nId:0; you will
> > > see where I attempt to save to the current record.
>
> > > [code]
>
> > > public function add_network($nId = 0)
> > > {
>
> > > // Capture for Debuging
> > > $_SESSION['nId'] = $nId;
>
> > > if($nId){
> > > $net_info = $this->Network->find('first',
> > > array('conditions' => array('id' => $nId)));
> > > self::set('network', $net_info);
> > > self::set('page_title', "View | Edit Network".
> > > $net_info['Network']['network_name']." ID: ".$_SESSION['nId']);
> > > } else {
> > > self::set('network', null);
> > > self::set('page_title', "Add Network");
> > > }
>
> > > $alert = null;
>
> > > if ($this->data['network']) {
> > > if ($this->data['network']['network_name']) {
> > > $data['network_name'] = addslashes($this-
> > >> data['network']['network_name']);
> > > $data['sub_id'] = addslashes($this->data['network']
> > > ['sub_id']);
> > > $data['active'] = 1;
>
> > > //Save form data to Networks database.
> > > //If we are editing a network, then make sure to save
> > > data to record ID ($nId)
> > > $mySaveId = ($nId)?$nId:0;
> > > $networkId = $this->Network->_save($mySaveId, $data);
>
> > > if ($networkId == $mySaveId) {
> > > $alert = "Changes to network: ".
> > > $data['network_name']." were saved.";
> > > } else {
> > > $alert = "New network: ".$data['network_name']."
> > > was added.". " ".$networkId;
> > > }
> > > }
> > > }
>
> > > switch($this->data['window']['flag']){
> > > case "networks":
> > > self::redirect("/admin/manage_network");
> > > break;
>
> > > } // switch
>
> > > self::set('logo', $this->Session->read('logo'));
> > > self::set('wbtitle', $this->Session->read('wbtitle'));
> > > self::set('alert', $alert);
> > > self::set('networkId', isset($networkId)?$networkId:null);
> > > }
>
> > > [/code]
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php