We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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'));


9.3k
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.phalcon.io/en/latest/api/Phalcon_Mvc_Model_RelationInterface.html