hlohrenz / CI4-Relationship-Query-Builder

This introduces relationship query building into CodeIgniter 4 along with scopes.
MIT License
46 stars 19 forks source link

Nothing happens #2

Open crustamet opened 5 years ago

crustamet commented 5 years ago

`<?php namespace App\Models\Frontpanel;

use App\Models\BaseModel;

class Menu extends BaseModel { protected $table = 'frontpanel_menus'; protected $primaryKey = 'id_menu'; protected $foreignKey = 'id_connector';

protected $returnType = 'object';

public function connectors()
{
    return $this->hasMany('connectors', 'App\Models\Frontpanel\Connectors');
}

}

class Connectors extends BaseModel { protected $table = 'frontpanel_connectors'; protected $foreignKey = 'id_menu'; protected $primaryKey = 'id_connector';

protected $returnType   = 'object';

public function menu()
{
    return $this->belongsTo('menu', 'App\Models\Frontpanel\Menu');
    // $this->belongsTo('propertyName', 'model', 'foreign_key', 'owner_key');
}

} `

inside Controller

$menu = new Menu(); print_r($menu->find(10)->connectors); die();

i get ERROR : Undefined property: stdClass::$connectors

BTW

BASEMODEL line 561 i modified this method public function getForeignKey() { return $this->foreignKey; }

it has no sense to hardcode it, so i changed so it would work with appended ids also id_menu, id_connector, ETC...

hlohrenz commented 5 years ago

I will be running this this evening to see if I can diagnose the issue!

crustamet commented 5 years ago

thanks i found some problems with the BaseModel that is extending made some debug stuff.

`public function findAll(int $limit = 0, int $offset = 0) { $this->applyScopes();

    $models = parent::findAll($limit, $offset);

    // If models returned, eager load relations
    if (count($models) > 0) {
        $models = $this->eagerLoadRelations([$this]);
    }
    // Return new collection of models
    return $models;
}`

this code when you getting the "$models" $models = parent::findAll($limit, $offset);

this does not returns the MODELS it is just returning the ITEMS from the TABLE

From here is passing to all other files THE ITEMS and not the MODELS

I dont know how this baseModel worked for you but you clearly have something else..

bhaskaraswamy commented 3 years ago

I got a error like this Attempt to read property \"id\" on array