将另一个控制器中的另一个字段添加到数组中

时间:2012-08-21 06:25:59

标签: mysql cakephp model controller

嘿,目前我们有一个arrray从一个表(关系)中选择所有,我们需要从该数组中的Accounts表中放入account_name。我们该怎么做?

我们的关系表有(id,receiver_id,sender_id,active,expiry_date)。 Receiver_id和sender_id都是account_id的外键。

目前它工作正常但打印接收者和发件人的ID,我们希望来自Account表的account_name代替。

这是我们的功能,视图和模型:

功能:

//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); 
    }

查看:

<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>

关系模型:

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 个答案:

答案 0 :(得分:0)

请注意,它不是来自另一个“控制器”的另一个字段,而是另一个模型。

有很多方法可以做到这一点:

您可以在find()方法上使用联接:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

或者你也可以使用bindModel(),我发现它特别有用:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly

搜索食谱!虽然有时候很乏味,但它确实非常有用。在我对Cake的工作方式有了一个很好的理解之前,我不得不深入讨论。