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

Feature: callable relation params #15158

Merged
merged 2 commits into from
Sep 27, 2020
Merged

Conversation

zsilbi
Copy link
Member

@zsilbi zsilbi commented Sep 23, 2020

Hello!

  • Type: new feature

In raising this pull request, I confirm the following:

Small description of change:

Modified Phalcon\Mvc\Model\Relation to accept callable params for model relations.

Example use case:

/**
 * @var DiInterface $container
 */
$container = $this->getDI();

$this->hasMany(
    'id', Documents::class, 'customerId', [
        'params' => function() use ($container) {
            /**
             * @var Languages $language
             */
            $language = $container->getShared('language');

            return [
                'languageId = :languageId:',
                'bind' => [
                    'languageId' => $language->id
                ]
            ];
        }
    ]
);

When language is changed in the dependency container, the relations will automatically use the new parameter.

Thanks,
zsilbi

@zsilbi zsilbi added 4.1.0 new feature request Planned Feature or New Feature Request labels Sep 23, 2020
@Jeckerson
Copy link
Member

Jeckerson commented Sep 23, 2020

But isn't it the same, when just pass an array directly inside 'params'?

/**
 * @var DiInterface $container
 */
$container = $this->getDI();

/**
  * @var Languages $language
  */
$language = $container->getShared('language');

$this->hasMany(
    'id', Documents::class, 'customerId', [
        'params' => [
                'languageId = :languageId:',
                'bind' => [
                    'languageId' => $language->id
                ]
            ]
    ]
);

@zsilbi
Copy link
Member Author

zsilbi commented Sep 23, 2020

@Jeckerson

The models are only initialized once and I need a way to keep these parameters up to date.

That function is evaluated every time before the relations are accessed, so if the language changes between queries in the container, the languageId in the params will be updated.

@zsilbi zsilbi merged commit cccda28 into phalcon:4.1.x Sep 27, 2020
@zsilbi zsilbi deleted the relation-params branch September 27, 2020 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature request Planned Feature or New Feature Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants