Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relations #592

Open
maruo34 opened this issue Apr 18, 2018 · 0 comments
Open

Relations #592

maruo34 opened this issue Apr 18, 2018 · 0 comments

Comments

@maruo34
Copy link

maruo34 commented Apr 18, 2018

how to do so through AR
and what am I doing wrong?

SELECT r.recipe_id as `id`,r.craft_chance as `chance`,i.i_image as `image`,i.i_market_hash_name as `name`,ir.rarity,it.type
                    FROM `recipes` as r
                        LEFT JOIN `items` as i ON (i.i_id = r.recipe_id)
                        LEFT JOIN `items_types` as it ON (it.id = i.i_type_id)
                        LEFT JOIN `items_rarityes` as ir ON (ir.id = i.i_rarity_id)
                    WHERE r.recipe_id = ?
namespace frontend\models;

class Recipe extends \ActiveRecord\Model {

    public static $table_name = 'recipes';

    public static $primary_key = 'recipe_id';

    public static $has_many = [
        [
            'items',
            'class_name' => \Item::class, 
            'primary_key' => 'recipe_id', 
            'foreign_key' => 'i_id',
            'select' => 'i_id, i_market_hash_name, i_image'
        ],
        [
            'items_types',
            'class_name' => \ItemType::class,
            'through' => \Item::class,
        ],
        [
            'items_rarityes',
            'class_name' => \ItemRarity::class,
            'through' => \Item::class,
        ],
    ];

    /**
     * Getting Recipe
     * @param int, $recipe_id -
     * @return array, [...] 
     */
    public function getRecipe(int $recipe_id) : array {
        $res = self::find('one',[
            'select' => 'recipes.*,items.*',
            'joins' => ['items','items_types','items_rarityes'],
            'conditions' => ['recipe_id' => $recipe_id],
        ]);`
namespace frontend\models;

class Item extends \ActiveRecord\Model {
    
    public static $table_name = 'items';
    
    public static $primary_key = 'i_id';

    public static $belongs_to  = [
        [
            'recipes',
            'class_name' => \Recipe::class, 
            'foreign_key' => 'recipe_id',
            'primary_key' => 'i_id'
        ],
        [
            'items_types', 
            'class_name' => \ItemType::class, 
            'foreign_key' => 'item_type_id',
            'primary_key' => 'i_id'
        ],
        [
            'items_rarityes', 
            'class_name' => \ItemRarity::class, 
            'foreign_key' => 'item_rarity_id',
            'primary_key' => 'i_id'
        ],
    ];

}
namespace frontend\models;

class ItemType extends \ActiveRecord\Model {
    
    public static $table_name = 'items_types';
    
    public static $primary_key = 'id';

    public static $belongs_to = [
        ['items', 'class_name' => \Item::class, 'foreign_key' => 'item_type_id']
    ];
}
namespace frontend\models;

class ItemRarity extends \ActiveRecord\Model {

    public static $table_name = 'items_rarityes';

    public static $primary_key = 'id';

    public static $belongs_to = [
        ['items', 'class_name' => \Item::class, 'foreign_key' => 'item_rarity_id']
    ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant