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.

Data order when fetching many-to-many

Hi there,

I am writing my own website, which is based on Phalcon Website project and I am using database as a source of the data.

I am implementing blog with posts and tags.

So each post can have many tags and each tag can be attached to multiple posts. So we have here many-to-many relationship.

I would like to know if there is a way to order tags alphabetically?

My code in Volt look like:

{% for _tags2post in _post.Tags2Posts %}
    {% if _tagIndex > 0 %},{% endif %}

    <a href="{{ language }}/{{ locale.translate('tags') }}/{{ _tags2post.tags.slug }}">
        {{ _tags2post.tags.name }}
    </a>

    {% set _tagIndex = _tagIndex + 1 %}
{% endfor %}

And one more question: how to higlight Volt code in this forum?



73.5k
Accepted
answer
    public function initialize()
    {
        $this->hasMany('id', 'Models\ServicesVideos', 'service_id', [
            'alias' => 'videos',
            'params' => [
                'order' => 'position ASC',
                'conditions' => 'type = :type:',
                'bind' => [
                    'type' => get_class($this)
                ]
            ]
        ]);

        $this->hasManyToMany(
            'id',
            'Models\EditionModules',
            'edition_id', 'module_id',
            'Models\Modules',
            'id', [
                'alias' => 'modules',
                'params' => [
                    'order' => 'ord ASC',
                    'conditions' => 'is_active = 1',
                    // 'cache' => ['lifetime' => 3600, 'key' => 'current-active-edition-modules']
                ]
            ]
        );
    }

Nice documentation can be found here: https://docs.phalconphp.com/en/3.2/db-models-relationships

Not sure about the syntax highlight on the forum. The guys were changing some stuff. Perhaps someone else can help with this :)

@NikolayMihaylov: those lines related to cache - can I use them with additional cache configuration or it should work just like that?

What do you mean by additional cache config? Talking about Redis, Filesytem and other cache drivers or something else?

I have following cache configuration in my bootstrap class:

    /**
     * Initializes the Cache
     */
    protected function initCache()
    {
        /**
         * viewCache
         */
        /** @var \Phalcon\Config $config */
        $config   = $this->diContainer->getShared('config');
        $lifetime = $config->get('cache')->get('lifetime', 3600);
        $driver   = $config->get('cache')->get('viewDriver', 'file');
        $frontEnd = new PhCacheFrontOutput(['lifetime' => $lifetime]);
        $cacheDir = $config->app->cacheDir;

        $backEnd  = ['cacheDir' => $cacheDir . '/view/'];
        $class    = sprintf('\Phalcon\Cache\Backend\%s', ucfirst($driver));
        $cache    = new $class($frontEnd, $backEnd);

        $this->diContainer->set('viewCache', $cache);

        /**
         * cacheData
         */
        $driver   = $config->get('cache')->get('driver', 'file');
        $frontEnd = new PhCacheFrontData(['lifetime' => $lifetime]);
        $backEnd  = ['cacheDir' => $cacheDir . '/data/'];
        $class    = sprintf('\Phalcon\Cache\Backend\%s', ucfirst($driver));
        $cache    = new $class($frontEnd, $backEnd);

        $this->diContainer->setShared('cacheData', $cache);
    }

Will this commented line be working with it?

I figure out how to highlight the code:

  • 'php' after ```
  • 'volt' after ```
edited 20d ago

Yes, it will work! :)

The cache key in the Relation Definition tells the ORM to cache the results using the whatever driver you defined.

However I don't see any modelsCache definition in your code. Here is how to set it:

// Models cache
$di->setShared('modelsCache', function() use ($config) {
    $frontCache = new \Phalcon\Cache\Frontend\Data(['lifetime' => 7200]); // 2h
    $cache = new \Phalcon\Cache\Backend\File($frontCache, [
        'cacheDir' => $config->site->path->cache . 'queries/'
    ]);
    if ($config->debug) {
        $cache->flush();
    }
    return $cache;
});