We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PhalconException: Table "Luckyparticipants" doesn't exist on database when dumping meta-data for Luckyparticipants

<?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;
    }
}


2.6k
Accepted
answer
edited Dec '14

solved:

<?php
    public function getSource()
    {
        return "luckyparticipants";
    }

I got the same error when on multi modules system through following commands : phalcon create-all-models --get-set --mapcolumn --directory apps\frontend\models In controller file along with this I got the error: $ForexRates = ForexRates::find(); print_r($ForexRates);

Got the error: Notice: Array to string conversion in C:\xampp\htdocs\testingtoday\apps\frontend\Module.php on line 78 Notice: Array to string conversion in C:\xampp\htdocs\testingtoday\apps\frontend\Module.php on line 78 Table 'forex_rates' doesn't exist in database when dumping meta-data for testingtoday\frontend\Models\ForexRates

And on following line changed the code : in testingtoday\apps\frontend Module.php

$di['db'] = function () use ($config) { // return new DbAdapter($config->toArray()); //}; To Replaces with : $di['db'] = function() use ($config) { // return new DbAdapter(array( "host" =>$config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname )); };