<?php
class Luckyparticipants extends Model {
public $id;
public $lucky_id;
public $user_id;
public function initialize()
{
$this->belongsTo("lucky_id", "Lucky", "id");
}
}
<?php
class Model extends \Phalcon\Mvc\Model {
public $di;
public $manager;
public $db;
public $tools;
public $session;
public $config;
/**
* Set DI and DB defaults of models
*/
public function onConstruct(){
$this->di = (object)\Phalcon\DI\FactoryDefault::getDefault();
$this->manager = $this->di['modelsManager'];
$this->db = $this->di['db'];
$this->tools = $this->di['tools'];
$this->config = $this->di['config'];
$this->session = $this->di['session'];
$this->models = $this->di['models'];
$this->setSource($this->db->getModelTable(get_called_class()));
}
/**
* Get data list
* @params - where params (string, array)
* @full - select all table columns or only columns wich in sys_heads table (bool)
* @editable - go to findFirst method (bool)
*/
public function get($params = null, $full = true, $editable = false, $model = null){
$model = ($model == null) ? get_called_class() : $model;
$fields = ($full) ? $this->db->getModelColumns($model) : $this->tools->getHead($model);
$columns = '';
$where = '';
$bind = array();
if (is_string($params)){
$colkey = ((intval($params)+1) == 1) ? 'UNI' : 'PRI';
$key = $this->db->getUniqueColumn($model, $colkey);
$param = $params;
$params = array($key => $param);
}
$counter = 0;
foreach ($fields as $c=>$field){
$columns .= ($full) ? $field['col'].((count($fields) > $counter+1) ? ', ' : '') : $c.((count((array)$fields) > $counter+1) ? ', ' : '');
$counter++;
}
if ($params !== null){
$counter = 0;
foreach ($params as $field=>$value){
$where .= $field.' = :'.$field.':'.((count($params) > $counter+1) ? 'AND ' : '');
$bind[$field] = $value;
$counter++;
}
}
$result = ($params == null) ? $this->db->getAll($columns, $model) : $model::findFirst(array($where, 'bind' => $bind), $editable);
return (count((array)$result) > 0) ? $result : false;
}
/**
* Redeclarade default Phalcon method, remove DI data in result
* @params - data (array)
* @editable - std object given or phalcon object. phalcon object may be save (bool)
*/
public static function findFirst($params = null, $editable = false){
$data = parent::findFirst($params);
if ($editable)
return $data;
$object = new stdClass();
if (count($data) > 0 && $data != null)
foreach($data as $k=>$r)
if (!is_object($r) && !preg_match('/^_[a-z]+/i', $k))
$object->$k = $r;
return $object;
}
/**
* Replace id values to names and return
* @date - date given (object)
* @column - column replace (string)
* @model - model name (string)
* @default - default array with values (array)
* @fieldName - field name (string)
* @fieldID - field id (string)
*/
public function setIDtoName($data, $column, $model, $default = null, $fieldName = 'name', $fieldID = 'id'){
$all = $this->db->getAll('*', $model);
$sorted = ($default == null) ? array() : $default;
if (count((array)$all) > 0)
foreach ($all as $a)
$sorted[$a->$fieldID] = $a->$fieldName;
if (count((array)$data) > 0)
foreach ($data as $d)
$d->$column = (isset($sorted[(int)$d->$column])) ? $sorted[(int)$d->$column] : 0;
return ($data != null) ? $data : false;
}
}