1
votes

I'm trying to get settings for a website from MongoDB via PhalconPHP, using an Phalcon\Mvc\Model. Here's my model:

namespace CMS4\Common\Model;

use Phalcon\Mvc\Model as MongoDocument;

class Settings extends MongoDocument
{
        /**
         *
         * @var \MongoId
         */
        protected $id;

        /**
         *
         * @var int
         */
        public $websiteId;

        /**
         *
         * @var array[]
         */
        public $leUserConfig;

        public function initialize()
        {
                $this->setConnectionService('mongo');
        }

        /**
         * By default that model will refer to `WebsiteSettings`, wherefore getSource is implemented
         * @return string
         */
        public function getSource()
        {
                return 'WebsiteSettings';
        }
}

However, calling it via $test = Settings::find(); I get the following RuntimeException:

Call to undefined method ::gettype()

I guess this is used somewhere internally by PhalconPHP, but a quick glance at the source code led me nowhere. I'm out of ideas, please advise. If I need to provide more information, just say so.

1
to my knowledge you need to pass something to find if you are trying to find by primary_key, or condition or where clause... olddocs.phalconphp.com/en/3.0.1/reference/odm.html - Gunnrryy
When working with Mongo, you should be extending Phalcon\Mvc\Collection instead of Model. You can learn more about how to use collections and the phalcon odm here: docs.phalconphp.com/en/3.2/db-odm - Armon Bigham
@Armon Bigham that worked, thanks. How can I add it as an accepted answer, I guess you must add it as an answer instead of a comment? - Veselin Bakov
@VeselinBakov Glad it worked. I have added it as an answer :) - Armon Bigham

1 Answers

0
votes

When querying a Mongo Collection with the Phalcon Framework, you should be using the ODM instead of the ORM, and as such, using the Collection classes instead of the Model classes.

You can find out more on how to set this up and use these features here: https://docs.phalconphp.com/en/3.2/db-odm