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