Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get ManyToMany metadata from model

How do I get the ManyToMany metadata from the current model?

In my User-model I have the following:

public function initialize() {
    $this->hasManyToMany("id", "Model\UserHasGroup", "user_id", "account_id", "Model\Group", "id", array('alias' => 'groups'));

I want the get these params in another function in my User-model:

    function testFunction($related_alias) {
        // How do I extract the params from hasManyToMany function above?
    }

    print_r(testFunction('groups'));


6.9k
Accepted
answer

Via modelsManager you can get

/**
 * Gets hasManyToMany relations defined on a model
 * @param \Phalcon\Mvc\ModelInterface $model
 * @return \Phalcon\Mvc\Model\RelationInterface[]
 */
public function getHasManyToMany(ModelInterface $model)

/**
 * Returns a relation by its alias
 * @param string $modelName
 * @param string $alias
 * @return \Phalcon\Mvc\Model\RelationInterface
 */
public function getRelationByAlias($modelName, $alias)

and from there, you can get the configuration https://docs.phalconphp.com/en/latest/api/Phalcon%5Mvc%5Model%5RelationInterface.html