We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Modeling and MongoDB integration

Hi Phalcon Expert,

I am working on my own project try to rebuild Magento 1 and 2 framework into Phalcon framework and make it as fast as possible and replacing the Product Catalog using MongoDB instead of mysql, but I found the modeling in Phalcon is not very flexible and I would like to get some help from the framework builder.

Firstly, in Phalcon for performance we need to predefine all the object attribute as properties like

    <?php
    namespace Company\Catalog;

    class Product extends \Phalcon\Mvc\Model
    {
        public $id;
        public $data;

        public function initialize()
        {
            //do other setSource to change table name and define behavior
        }

        //other userful mapping function but I don't know when it was called
        public function columnMap()
        {
            return array(
                'id' => 'entity_id',
                'name' => 'name',
                'sku' => 'sku'
            );
        }

        //map the mysql table name or mongodb collection name
        public function getSource()
        {
            return "product_entity";
        }
    }

Is there any way that I could do an associated array for all the attributes of the model like this

    <?php
    namespace Company\Catalog;

    class Product extends \Phalcon\Mvc\Model
    {
        //it is impossible to list all properties as our model is dynamic document or EAV database
        public $name;
        public $attributeSet;
        public $sku;
        pulbic $productType;

        public function initialize()
        {
            //do other setSource to change table name and define behavior
        }

        //other userful mapping function but I don't know when it was called
        public function columnMap()
        {
            return array(
                'local_name' => 'table_column_name',
                'local_name_2' => 'table_column_name_2'
            );
        }

        //map the mysql table name or mongodb collection name
        public function getSource()
        {
            return "product_entity";
        }

        public function getData($key = '')
        {
            if (isset($this->data[$key])) {
                return $this->data[$key];
            }
            return null;
        }

        public function setData($key, $value = null)
        {
            if (!array_key_exists($key, $this->data) || $this->data[$key] !== $value) {
                $this->hasDataChanges = true;
            }
            $this->data[$key] = $value;     
        }

        public function __call($method, $args)
        {
            switch (substr($method, 0, 3)) {
            case 'get':
                $key = $this->_underscore(substr($method, 3));
                $index = isset($args[0]) ? $args[0] : null;
                return $this->getData($key, $index);
            case 'set':
                $key = $this->_underscore(substr($method, 3));
                $value = isset($args[0]) ? $args[0] : null;
                return $this->setData($key, $value);
            case 'uns':
                $key = $this->_underscore(substr($method, 3));
                return $this->unsetData($key);
            case 'has':
                $key = $this->_underscore(substr($method, 3));
                return isset($this->data[$key]);
            }
            throw new Exception(
                sprintf('Invalid method %s::%s(%s)', get_class($this), $method, print_r($args, 1))
            );
        }
    }

I know there is good ODM implementation of Phalcon to MongoDB but it is not documented well and it is very hard to find example. and Now PHP already has MongoClient and it has all the function I need to do complex query like mapReduce and custom javascript filter, but I could not find any in Phalcon, please help, as I need to make it fast in Phalcon so I need to do it in Phalcon way. Thank you very much



2.6k

I could do this to assign whatever attributes I want for the model but I am not sure is this the fastest way or good way as I find out I am not leverage the framework much and if I don't use the C language code then it will be as slow as pure PHP code

    <?php
    class Product extends \Phalcon\Mvc\Model
    {
        public function beforeSave()
        {
            $this->assign(array(
                'sku' => 'ABCD123',
                'name' => 'LG Smart 3D',
                'attributeSet' => TV,
                'size' => '56 inch'
            ));
        }
    }

I also want to know when the validation function was called as I might need to write the custom validation for each model. As I will have a attribute model to control product model attributes, so I need to know how you map the columns in database and in mongoDB when Phalcon talk to the databases. Thanks

edited Dec '16

Hey Ben!

I am too curious about phalcon framework. Being a Java developer couldnt get muh hands on php frameworks. Could u pls help me understand phalon. It seems one of the best design available (work is still going on). I am also looking phalcon with MoongoDB. How efficient and scalable the combination is, when we talk about migrating magento DB or to design a scalable application. Help is appreciated! :)