dongyishen5796 2015-01-21 16:03
浏览 9
已采纳

巴黎ORM,通过限制

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.

  • 写回答

1条回答 默认 最新

  • dpepbjp126917 2015-01-21 16:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?