2

現時点では、1 つのテーブル (リレーションシップ) からすべてを選択する配列があり、その配列に Accounts テーブルから account_name を配置する必要があります。どうすればそうしますか?

Relationship テーブルには (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;
    }

}
4

1 に答える 1

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-急いで

料理本を探しましょう!退屈な場合もありますが、非常に役立ちます。Cake がどのように機能するかを十分に理解するには、かなりの時間を割く必要がありました。

于 2012-08-22T14:35:10.033 に答える