Search code examples
phpidiorm

Paris ORM, has_many_through with restrictions


What's the best way to approach this in Paris ORM?

I have a set of categories and a set of supplier profiles that havw a column called reatured. Currently my class is as follows:

<?php

namespace {

    /**
     * Class Category
     */
    class Category extends ConfettiModel
    {
        public static $_table = 'supplier_directory_category';

        /**
         * Returns only top level categories - they have no parent
         *
         * @return bool
         */
        public static function topLevel()
        {
            return self::where('parent', 0);
        }

        public static function marketing()
        {
            return self::where('marketing', 'Yes');
        }

        public function getTable() {
            return self::$_table;
        }

        /**
         * Is this a top level category - has no parent
         *
         * @return bool
         */
        public function isTopLevel()
        {
            return ($this->parentId == 0);
        }

        /**
         * Associated DirectoryProfile's
         *
         * @return ORMWrapper
         */
        public function profiles()
        {
            return $this->has_many_through('DirectoryProfile', 'CategoryDirectoryProfile', 'category', 'supplier');
        }
}

I'd like to add a new function, featuredProfiles() that allows me to retrieve the same results as profiles(), but in this case I want to restrict it to suppliers with featured = 'Yes'.

I'm not quite sure how to make this happen.


Solution

  • I took a punt and the answer was easier than I anticipated:

    public function featuredProfiles() {
        return $this->profiles()->where('featured', 'Yes');
    }
    

    The where is added as part of the query on the joined table.