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 8258957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:43:36+00:00 2026-06-08T02:43:36+00:00

I have a site developed in cakephp 2.0, I want to make a relation

  • 0

I have a site developed in cakephp 2.0, I want to make a relation between two tables:

activity_ingredients

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   activity_id     int(11)     No  None        
4   ingredient_id   int(10)     No  None        
5   created     datetime        

actions

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   language    char(2)     No  None        
4   text    varchar(100)        No  None        
5   created     datetime    

I want to associate the two tables with the field “type_id”.
I have done in this mode into my code:

    class Action extends AppModel{
    public $name = 'Action'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

    public $belongsTo = array(
        'ActivityIngredients' => array(
            'className'     => 'ActivityIngredients',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'type_id'
        )
    );

}

class ActivityIngredients extends AppModel{
        public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

        public $belongsTo = array(
            'Activity' => array(
                'className'     => 'Activity',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'activity_id'
            ),
            'Ingredient' => array(
                'className'     => 'Ingredient',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'ingredient_id'
            )
        );

        public $hasMany = array(
            'Action' => array(
                'className' => 'Action',
                'conditions' => '',
                'dependent' => true,
                'foreignKey'   => 'type_id',
                'associatedKey'   => 'type_id'
            )
        );
    }

It doesn’t retrieve the correct data.. It seems that It takes the id for the foreign key.
This is the view:

<?php foreach ($user['Activity'] as $activities) {
var_dump($activities);
?>
    <div class="line-cnt"><div class="line">
    </div>
</div>
<h2>Attività</h2>
<div class="table">
    <div>
        <div>Activity created</div><div><?php echo $activities['created']; ?>
        </div>
    </div>
    <div>
        <div>Actions text</div><div><?php echo $activities['Action']['text']; ?></div>
    </div>
    <div>
        <div>ActivityIngredient ingredient_id</div><div><?php echo $activities['ActivityIngredients']['ingredient_id']; ?></div>
    </div>
</div>
<?php
}
?>

The controller is a simple query with find all and recursive 3 into the User that is collegate with the tables

$this->User->recursive = 3;
        $user = $this->User->read();

        if (empty($username) || $username != $user['User']['username']) {
            $this->redirect(array ('action'=>'view',$id,$user['User']['username']));
        }

        $this->set('user', $user);

Help me please

  • 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-08T02:43:39+00:00Added an answer on June 8, 2026 at 2:43 am

    The first thing if you are using an “id” field in your “activity_ingredients” table, then you should use it as a foreignKey in another table.

    A foreign key is a field in a relational table that matches a candidate key of another table.

    Even if you are trying to use type_id as foreign key in “actions” table, Then type_id must be unique in your activity_ingredients table, and if it is so then you can define your ActivityIngredient Model as:

    class ActivityIngredients extends AppModel{
        public $primaryKey = 'type_id';
        public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità
    
        public $belongsTo = array(
            'Activity' => array(
                'className'     => 'Activity',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'activity_id'
            ),
            'Ingredient' => array(
                'className'     => 'Ingredient',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'ingredient_id'
            )
        );
    
        public $hasMany = array(
            'Action' => array(
                'className' => 'Action',
                'conditions' => '',
                'dependent' => true,
                'foreignKey'   => 'type_id',
                'associatedKey'   => 'type_id'
            )
        );
    }
    

    And your Action Model will remain the same. And hence you will be able to fetch the desired records.

    And even if you are not agree to define “type_id” as foreign key in your table. Then this code will work extremely well with your situation.

    class ActivityIngredients extends AppModel{
    public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità
    
    public $belongsTo = array(
        'Activity' => array(
            'className'     => 'Activity',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'activity_id'
        ),
        'Ingredient' => array(
            'className'     => 'Ingredient',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'ingredient_id'
        )
    );
    
    public $hasMany = array(
        'Action' => array(
            'className' => 'Action',
            'conditions' => '',
            'dependent' => true,
            'foreignKey'   => false,
            'finderQuery'   => 'select * from actions as `Action` where
                                `Action`.`type_id` = {$__cakeID__$} '
        )
    );
    

    }

    I am sure this will give you the desired result. Kindly ask if it not worked for you.

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

Sidebar

Related Questions

I have a site develop in cakephp 2.0 I want to make a HABTM
I have a site developed in cakephp 2 and I want that into my
I have a site developed in cakephp 2.0, I have some tables relationed here
I have a site developed in cakephp 2.0 and I want to see, when
I have a site develop in cakephp 2.0. I have a relation HABTM to
Hi I have a site develop in cakephp. I want to connect my cakephp
I have a site developed in Opencart.Now I want to edit the heading title
I have a site developed with cakePHP. I have it under source control with
I have a site develop in cakephp 2.x I want into my controller call
I have a site developed with django at www.example.com I want that django insert/serve

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.