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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:00:33+00:00 2026-06-06T01:00:33+00:00

I’m using cakephp 2.1.3 and I have a table Ingredients and a table IngredientAliases

  • 0

I’m using cakephp 2.1.3 and I have a table Ingredients and a table IngredientAliases
I want that in my page index in Ingredient when I select an IngredientAlias I want to see the IngredientAlias and some fileds of Ingredients
But I don’t know if I have to make two query into my controller or only to set ingredients_id the id and cakephp in automatically retrieve me the data. Now I make two query in my controller but I don’t know if it’s the best way

$this->set('ingredient', $this->Ingredient->read());            $this->set('ingredient_alias',$this->Ingredient->IngredientAlias->read()); 

Here is my tables:

class Ingredient extends AppModel {
    public $name = 'Ingredient';
    public $useTable = 'ingredients';
    public $belongsTo = 'User';

    public $hasMany = array (
        'IngredientAlias' => array (
            'className'     => 'IngredientAlias',
            'foreignKey'    => 'ingredient_id'
        )
    );
}

class IngredientAlias extends AppModel {
    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
        'Ingredient' => array(
            'className'    => 'Ingredient',
            'foreignKey'   => 'ingredient_id'
        )
    );
}

And here is my IngredientsController (I have changed the routes to putt alias into my arguments and it’s working)

public function edit ($alias) {
        $ing = $this->Ingredient->IngredientAlias->find('first', array(
                    'conditions' => array('IngredientAlias.alias' => $alias)));
        $this->Ingredient->IngredientAlias->id= $ing['IngredientAlias']['id'];
        $this->Ingredient->IngredientAlias->set('ingredient_id',$ing['IngredientAlias']['ingredient_id']);
        $this->Ingredient->id= $ing['IngredientAlias']['ingredient_id'];
        if (!$this->Ingredient->IngredientAlias->exists()) {
                throw new NotFoundException ('Nessuna corrispondenza trovata per questo ingrediente');
            }
            if (!$alias) {
                $this->set('flash_element','error');
                $this->Session->setFlash ('Ingrediente non valido');
            }
            $this->Ingredient->recursive = 2;
            $this->set('ingredient', $this->Ingredient->read());
            $this->set('ingredient_alias',$this->Ingredient->IngredientAlias->read());
    }
  • 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-06T01:00:35+00:00Added an answer on June 6, 2026 at 1:00 am

    Based on the relations you described and if you have a ingredient_id key in your ingredient_aliases table if you call:

    $this->Ingredient->find('first', array('conditions' => array('Ingredient.id' => $id)));
    

    You would get the Ingredient record specified by $id with all it’s associated IngredientAlias records in an array structure similar to the following:

    Array =>
        [Ingredient] => Array
                            [id] => 3
                            [other_data_from_DB] =>
                            .....
        [IngredientAlias] => Array => 
                             [0] => Array =>
                                    [id] => 1
                                    [ingredient_id] => 3
                                    [other_data_from_DB] =>
                                    ....
                             [1] => Array =>
                                    [id] => 2
                                    [ingredient_id] => 3
                                    [other_data_from_DB] =>
                                    ....
                             [2] => Array =>
                                    [id] => 3
                                    [ingredient_id] => 3
                                    [other_data_from_DB] =>
                                    ....
    

    The IngredientAlias array sub-structure is indexed because of the hasMany relation.
    If you’re not seeing the associated IngredientAlias records then check your Model’s recursive property:

    debug($this->Ingredient->recursive);
    

    If it equals -1 then only the Ingredient Model data will be fetched. Check out what the other recursive values do in the link I provided.

    If you want to go the other way around you should go through the IngredientAlias model:

    $this->IngredientAlias->find('first', array(CONDITIONS));
    

    will provide the specified in the CONDITIONS IngredientAlias record with it’s connected Ingredient record. The Ingredient record will be only one because of the IngredientAlias belongsTo Ingredient relation.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.