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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:55:12+00:00 2026-06-10T03:55:12+00:00

Hey at the moment we have an arrray selecting all from one table (relationship)

  • 0

Hey at the moment we have an arrray selecting all from one table (relationship) and we need to put account_name from the Accounts table in that array. How would we do so?

Our Relationship table has (id, receiver_id, sender_id, active, expiry_date). Receiver_id and sender_id both are the foreign keys for account_id.

At the moment it works fine but prints the ids of the receiver and sender, we want the account_name from Account table to be there instead.

Here is our function, view and model:

Function:

//retrieve Account Id of current User       
        $accountid=$this->Auth->user('account_id');

        //Conditions
        $conditions=array("OR"=> array(
        'Relationship.sender_id' => $accountid,
        'Relationship.receiver_id' => $accountid));

        //Find all Invoices where receiver_id = accountid
        $relationships=$this->Relationship->find('all', array(
        'conditions' => $conditions));

        debug($relationships);

        $this->set('accountid', $accountid); 
        $this->set('relationship', $relationships); 
    }

View:

<table>
                <tr>
                    <th>Relationship #</th>
                    <th>Sender</th>
                    <th>Receiver</th>
                    <th>Expiry Date</th>
                    <th>Status</th>
                </tr>

        <?php foreach($relationship as $relationships):?>

        <?php

        if($relationships['Relationship']['active']==1)
        {
            $status = 'Active';
        }
        else if($relationships['Relationship']['active']==0)
        {
            $status = 'Not Active';
        }


?>

                    <tr>
                        <td align='center'><?php echo $relationships['Relationship']['id']; ?></td>
                        <td align='center'><?php echo $relationships['Relationship']['sender_id']; ?></td>
                        <td align='center'><?php echo $relationships['Relationship']['receiver_id']; ?></td>
                        <td align='center'><?php echo date('d.m.Y', strtotime($relationships['Relationship']['expiry_date'])); ?></td>
                        <td align='center'><?php echo $status ?></td>
                    </tr>
                <?php endforeach; ?>


            </table>

Relationship Model:

class Relationship extends AppModel
{

    var $name = 'Relationship';
    public $useTable = 'relationships';
    public $primaryKey = 'id';
    /*
    public $hasMany = array(
        'Invoice' =>
            array(
                'className'              => 'Invoice',
                'joinTable'              => 'invoice',
                'foreignKey'             => 'invoice_id'));*/

    //fix this code
    public $belongsTo = array(
        'User' =>array(
            'className' => 'User',
            'foreignKey' =>'receiver_id',
            'associationForeignKey'  => 'accounts_id',
            )); 

    var $validate = array(
        'date' => array(
            'rule' => array(
                'datevalidation',
                'systemDate'
            ),
            'message' => 'Current Date and System Date is mismatched'
        ),
        'receiver_id' => array(
            'userExists' => array(
                'rule' => array(
                    'userExists',
                ),
                'message' => 'That username doesnt exist.'
            ),
        ),
    );

    function datevalidation($field = array(), $compare_field = null)
    {
        if ($field['date'] > $compare_field)
            return TRUE;
        else
            return FALSE;
    }

    function accountExists($check)
    {   
        $accountExists = $this->Account->find('count', array('conditions' => array('Account.id'=>$check)));
        if ($accountExists == 1) {
           return TRUE;
        }
        else
            return FALSE;
    }

}
  • 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-10T03:55:14+00:00Added an answer on June 10, 2026 at 3:55 am

    Note it’s not another field from another “controller” but rather another model.

    There are a number of ways you could do this:

    You could use joins on the find() method: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

    Or you could probably also use bindModel(), which I find particularly useful: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly

    Scour the cookbook! Although sometimes tedious, it really is extremely helpful. I had to hack around quite a bit before I got a decent understanding of how Cake works.

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

Sidebar

Related Questions

I need a linq statement that returns all entries from a certain date. A
Hey all, I have a basic HTML contact form that goes to a .php
Hey so at the moment in my table I have a column called hash
Hey all. Basically I have a page full of text input fields with default
Hey guys another rails issue, Currently have a collection that is line items for
Hey I have seen sites that instead of an int show you a string.
I have a very simple script that slides a message down from the top
Hey I have a wordpress blog, and I need to add some negative margin
Hey All you out there, I am trying to write a query that sums
Hey all, I'm developing a rhythm game for the iPhone at the moment, just

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.