Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:30:14+00:00 2026-06-15T17:30:14+00:00

I have two models Ficha and Perigo: class Ficha extends AppModel { var $primaryKey

  • 0

I have two models “Ficha” and “Perigo”:

class Ficha extends AppModel {

    var $primaryKey = 'id_ficha';

    public $validate = array(
        'nome_produto' => array(
            'rule' => 'notEmpty'
        ),
        'versao' => array(
            'rule' => 'notEmpty'
        ),
        'data_ficha' => array(
            'rule' => 'notEmpty'
        )
    );
}


    class Perigo extends AppModel {


    var $belongsTo = array(
        'Ficha' => array(
            'className'    => 'Ficha',
            'foreignKey'   => 'id_fichas'
        )
    );
}

As you can see they are “linked”. Now, in the code i have a form for Ficha that the method “add()” of FichasController redirects to my Perigo Model:

add() (of FichaController)

    public function add() {
//pr($this->request->data); // to get the data from the form

    if ($this->request->is('post')) {
        $this->Ficha->create();
        if ($this->Ficha->save($this->request->data)) {
            $this->Session->setFlash('Your post has been saved.');
            //$last_id=$this->Ficha->getLastInsertID();
            //$this->redirect(array('action' => 'preencher_ficha'),$last_id);
            $this->redirect(array('controller'=>'perigos', 'action' => 'add'),$last_id);
        } else {
            $this->Session->setFlash('Unable to add your post.');
        }

    }
}

The redirection is made to a form that exists in PerigosController.

add.ctp (of Perigo)

echo $this->Form->create('Perigo');
echo $this->Form->input('class_subst', array('label' => 'Classificação da substância ou mistura:'));
echo $this->Form->input('simbolos_perigo', array('label' => 'Símbolos de Perigo:'));
echo $this->Form->input('frases_r', array('label' => 'Frases R:'));
echo $this->Form->end('Avançar');

add() (of PerigoController)

    public function add() {

    if ($this->request->is('post')) {
        $this->Perigo->create();
        if ($this->Perigo->save($this->request->data)) {
            $this->Session->setFlash('Your post has been saved.');
            $this->redirect(array('controller'=>'perigos', 'action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to add your post.');
        }

    }
}   

but there’s something i don’t know how to do it. Since the models are relational, and the same happens with the two tables on the database (the table perigos has a foreignkey that is the primary key of the table “fichas”, how can i insert the data on table perigos in database? I mean, how can i get the key of the Ficha inserted in the first form and insert her in the foreign key of “perigos” when i submit this second form?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:30:15+00:00Added an answer on June 15, 2026 at 5:30 pm

    As @mark says, your redirection is bad, it should include the id in the URL array:

    $this->redirect(array('controller'=>'perigos', 'action' => 'add', $last_id));
    

    Like this you are passing the parameter by URL.
    In the add action of your PerigosController you should have an $idFicha param at the method:

    //PerigosController.php
    public function add($idFicha){
        //here you can use $idFicha
        ...
    
        //when submiting the Perigos form, we add the foreign key.
        if($this->request->is('post')){
    
            //adding the foreign key to the Perigo's array to add in the DB.
            $this->request->data['Perigo']['ficha_id'] = $idFicha;
    
            if ($this->Ticket->save($this->request->data)) {
              //...
            }
        }
    }
    

    Data submitted by POST method in a form will be always contained in an array of this type: $this->request->data['Model_name']. In your case:

    $this->request->data['Perigo']
    

    Another way to do this, is using sessions if you prefer to hide the id value:
    http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#sessions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: public class ProfessorModels { public string FullName { get; set;
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models in Django linked together by ManyToMany relation like this: class
I have two models: class Video(models.Model): slug = models.SlugField(blank=True, default='', db_index=True, unique=True) class VideoTranslation(models.Model):
I have two models: Play and PlayParticipant , defined (in part) as: class PlayParticipant(models.Model):
I have two models: class Sentence < ActiveRecord::Base attr_accessible :sentence_id, :authority_name #... end class
I have two models, Category and Post. Category.rb class Category include Mongoid::Document field :title,
I have two models in separate apps: # Groups app class Group(models.Model): name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.