2
votes

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
What is the output of $relationships? - Arun Jain

1 Answers

0
votes

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.