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
Closed

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

thxer opened this issue Aug 26, 2015 · 7 comments
Labels
bug A bug report status: medium Medium

Comments

@thxer
Copy link

thxer commented Aug 26, 2015

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

@andresgutierrez andresgutierrez added the need script to reproduce Script is required to reproduce the issue label Aug 26, 2015
@thxer
Copy link
Author

thxer 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
Copy link
Author

thxer commented Oct 7, 2015

Any info?

@sergeyklay sergeyklay self-assigned this Oct 16, 2016
@sergeyklay
Copy link
Contributor

I'll try to sort out

@idevelop4you
Copy link

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

@sergeyklay sergeyklay removed the need script to reproduce Script is required to reproduce the issue label Jan 4, 2018
@idevelop4you
Copy link

idevelop4you 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
Copy link

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 Stale issue - automatically closed label Jul 31, 2018
@stale stale bot closed this as completed Aug 1, 2018
@sergeyklay sergeyklay reopened this Aug 1, 2018
@stale stale bot removed the stale Stale issue - automatically closed label Aug 1, 2018
@stale stale bot added the stale Stale issue - automatically closed label Oct 30, 2018
@niden niden closed this as completed Oct 30, 2018
@niden niden reopened this Oct 30, 2018
@stale stale bot removed the stale Stale issue - automatically closed label Oct 30, 2018
@stale stale bot added the stale Stale issue - automatically closed label Jan 29, 2019
@phalcon phalcon deleted a comment from stale bot Jan 29, 2019
@niden niden removed the stale Stale issue - automatically closed 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
Copy link
Member

niden commented May 17, 2019

Resolved in #14086

@niden niden closed this as completed May 17, 2019
@niden niden added the 4.0 label Jun 21, 2019
@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

6 participants