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

ModelsManager->getRelations doesn't return the many through relations #10839

Closed
thxer opened this issue Aug 26, 2015 · 7 comments

Comments

Projects
6 participants
@thxer
Copy link

commented Aug 26, 2015

I think it's missing from phalcon/mvc/model/manager.zep getRelations function.

@thxer

This comment has been minimized.

Copy link
Author

commented Aug 27, 2015

<?php

$di = new Phalcon\DI();

$di->set(
    'db',
    new Phalcon\Db\Adapter\Pdo\Sqlite(
        array(
            "dbname" => "sample.db"
        )
    )
);

$di->set('modelsManager', new Phalcon\Mvc\Model\Manager());

// Use the memory meta-data adapter or other
$di->set('modelsMetadata', new Phalcon\Mvc\Model\Metadata\Memory());

class Event extends \Phalcon\Mvc\Model{

    protected $id;

    protected $user_id;

    protected $text;

    public function initialize(){
        $this->belongsTo("user_id", "User", "id", ["alias"=>"user"]);
    }

}

class User extends \Phalcon\Mvc\Model{

    protected $id;

    protected $name;

    public function initialize(){
        $this->hasManyToMany("id", "UserGroup", "user_id", "group_id", "Group", "id", ["alias"=>"groups"]);
        $this->hasMany("id", "Events", "user_id", ["alias"=>"events"]);
    }

}

class UserGroup extends \Phalcon\Mvc\Model{

    protected $group_id;

    protected $user_id;

}

class Group extends \Phalcon\Mvc\Model{

    protected $id;

    protected $name;

    public function initialize(){
        $this->hasManyToMany("id", "UserGroup", "group_id", "user_id", "User", "id", ["alias"=>"users"]);
    }
}

// Result
var_dump((new User)->getModelsManager()->getRelations("User"));

// Many to many
var_dump((new User)->getModelsManager()->getHasManyToMany((new User)));

// Expected result for getRelations()
var_dump(array_merge((new User)->getModelsManager()->getRelations("User"), (new User)->getModelsManager()->getHasManyToMany((new User))));

Output


array(1) {
  [0]=>
  object(Phalcon\Mvc\Model\Relation)#12 (8) {
    ["_type":protected]=>
    int(2)
    ["_referencedModel":protected]=>
    string(6) "Events"
    ["_fields":protected]=>
    string(2) "id"
    ["_referencedFields":protected]=>
    string(7) "user_id"
    ["_intermediateModel":protected]=>
    NULL
    ["_intermediateFields":protected]=>
    NULL
    ["_intermediateReferencedFields":protected]=>
    NULL
    ["_options":protected]=>
    array(1) {
      ["alias"]=>
      string(6) "events"
    }
  }
}
array(1) {
  [0]=>
  object(Phalcon\Mvc\Model\Relation)#11 (8) {
    ["_type":protected]=>
    int(4)
    ["_referencedModel":protected]=>
    string(5) "Group"
    ["_fields":protected]=>
    string(2) "id"
    ["_referencedFields":protected]=>
    string(2) "id"
    ["_intermediateModel":protected]=>
    string(9) "UserGroup"
    ["_intermediateFields":protected]=>
    string(7) "user_id"
    ["_intermediateReferencedFields":protected]=>
    string(8) "group_id"
    ["_options":protected]=>
    array(1) {
      ["alias"]=>
      string(6) "groups"
    }
  }
}
array(2) {
  [0]=>
  object(Phalcon\Mvc\Model\Relation)#12 (8) {
    ["_type":protected]=>
    int(2)
    ["_referencedModel":protected]=>
    string(6) "Events"
    ["_fields":protected]=>
    string(2) "id"
    ["_referencedFields":protected]=>
    string(7) "user_id"
    ["_intermediateModel":protected]=>
    NULL
    ["_intermediateFields":protected]=>
    NULL
    ["_intermediateReferencedFields":protected]=>
    NULL
    ["_options":protected]=>
    array(1) {
      ["alias"]=>
      string(6) "events"
    }
  }
  [1]=>
  object(Phalcon\Mvc\Model\Relation)#11 (8) {
    ["_type":protected]=>
    int(4)
    ["_referencedModel":protected]=>
    string(5) "Group"
    ["_fields":protected]=>
    string(2) "id"
    ["_referencedFields":protected]=>
    string(2) "id"
    ["_intermediateModel":protected]=>
    string(9) "UserGroup"
    ["_intermediateFields":protected]=>
    string(7) "user_id"
    ["_intermediateReferencedFields":protected]=>
    string(8) "group_id"
    ["_options":protected]=>
    array(1) {
      ["alias"]=>
      string(6) "groups"
    }
  }
}
@thxer

This comment has been minimized.

Copy link
Author

commented Oct 7, 2015

Any info?

@sergeyklay sergeyklay self-assigned this Oct 16, 2016

@sergeyklay

This comment has been minimized.

Copy link
Member

commented Oct 16, 2016

I'll try to sort out

@idevelop4you

This comment has been minimized.

Copy link

commented Nov 27, 2017

Hello, any info on this? It seems still present.

@idevelop4you

This comment has been minimized.

Copy link

commented Feb 18, 2018

This issue is open from more than 2 years but seems that no-one is interested on it.

It could be solved by adding this piece of code in the getRelations method:

/**
  * Get has-many-to-many relations
 */
if fetch relations, this->_hasManyToManySingle[entityName] {
	for relation in relations {
		let allRelations[] = relation;
	}
}

Meanwhile I made it working by extending the original ModelsManager and overriding the getRelations method in order to add the many-to-many relation to the returned list:

<?php

class Manager extends ModelManager {  
    public function getRelations($modelName) {  
       $relations = parent::getRelations($modelName);  
       $entity = strtolower($modelName);  
       if(array_key_exists($entity, $this->_hasManyToManySingle)){  
           foreach ($this->_hasManyToManySingle[$entity] as $relation){  
               $relations[] = $relation;  
           }  
       }  
       return $relations;  
    }  
}  
@robinbertram

This comment has been minimized.

Copy link

commented May 2, 2018

I just bumped into this as well. Your workaround works fine @idevelop4you. But obviously it would be nice if this was fixed.

Ping: @sergeyklay

@stale stale bot added the stale label Jul 31, 2018

@stale stale bot closed this Aug 1, 2018

@sergeyklay sergeyklay reopened this Aug 1, 2018

@stale stale bot removed the stale label Aug 1, 2018

@stale stale bot added the stale label Oct 30, 2018

@niden niden closed this Oct 30, 2018

@niden niden reopened this Oct 30, 2018

@stale stale bot removed the stale label Oct 30, 2018

@stale stale bot added the stale label Jan 29, 2019

@phalcon phalcon deleted a comment from stale bot Jan 29, 2019

@niden niden removed the stale label Jan 29, 2019

@phalcon phalcon deleted a comment from stale bot Feb 3, 2019

@sergeyklay sergeyklay removed their assignment Feb 3, 2019

@phalcon phalcon deleted a comment from stale bot Feb 15, 2019

@niden niden added the Bug - Medium label Feb 15, 2019

@niden niden added this to To do in 4.0 Release via automation Feb 15, 2019

@niden niden referenced this issue May 17, 2019

Merged

T10839 many to many relationships #14086

3 of 4 tasks complete

@niden niden moved this from To do to In progress in 4.0 Release May 17, 2019

@niden

This comment has been minimized.

Copy link
Member

commented May 17, 2019

Resolved in #14086

@niden niden closed this May 17, 2019

4.0 Release automation moved this from In progress to Done May 17, 2019

@niden niden added the 4.0 label Jun 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.