PHP code example of alexinator1 / yii2-jta

1. Go to this page and download the library: Download alexinator1/yii2-jta library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexinator1 / yii2-jta example snippets


class User extends \alexinator1\jta\ActiveRecord
{
    ....
}

class Group extends \alexinator1\jta\ActiveRecord
{
    ....
}

class Group extends \alexinator1\jta\ActiveRecord
{
    ...
    
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])
            ->viaTable('user_group', ['group_id' => 'id'], null, ['role', 'joined_at']);
    }

    ...
}

    $group = Group::findOne($groupId);
    foreach($group->users as $user)
    {
        $role = $user->role[$group->id];
        $joinDate = $user->joined_at[$group->id];
        ...
    }

    $group = Group::find()->where($groupId)->with('users')->all();
    foreach($group->users as $user)
    {
        $role = $user->role[$group->id];
        $joinDate = $user->joined_at[$group->id];
        ...
    }

    $groups =  Group::find()->joinWith('users')->all();

    foreach($groups as $group){
        foreach($group->users as $user)
        {
            $role = $user->role[$group->id];
            ...
        }
    }

    $group = Group::find()
        ->with('users')
        ->where($groupId)
        ->asArray()
        ->one();
    foreach($group['users'] as $user)
    {
        $role = $user['role'];
        $joinDate = $user['joined_at'];
        ...
    }