Table "pages" doesn't exist on database when dumping meta-data for Pages

I am relatively new to Phalcon and am running into this error when trying to save a new record. I have narrowed down to this page case where the error still happens. The error is: Phalcon\Mvc\Model\Exception: Table "pages" doesn't exist on database when dumping meta-data for Pages

The core code is below. The error shows up at the save() line in the controller's saveAction(). Any help would really be appreciated.

Thanks!

A table with one column:

CREATE TABLE `pages` (
  `username` varchar(120) CHARACTER SET latin1 NOT NULL
)

Model:

<?php
use Phalcon\Mvc\Model;

class Pages extends Model
{
    public $username;

    public function getSource()
    {
        return 'pages';
    }   
}

The Controller:

<?php
class SignupController extends \Phalcon\Mvc\Controller
{
    public function saveAction()
    {
        $page = new Pages();

        //Store and check for errors
        $success = $page->save($this->request->getPost(), array('pages'));
    }
}


19.6k

You don't need to do that declarations in your model (it will assign the table and the columns automatically).

And about the error... did you add in the autoloader the models folder?



2.4k

Thanks for the reply.

Yes, I have added an autoloader for the models folder. There is no error creating the Pages object, so I assume that is working fine. The error only shows up when I try to save the object.

Also, the stacktrace of the error is below. I think line #1 looks a bit weird and suspicious.

#0  Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Pages: 12), Object(Phalcon\DI\FactoryDefault))
#1  Phalcon\Mvc\Model\MetaData->_initialize(Object(Pages: 12), pages-pages, pages, null)
#2  Phalcon\Mvc\Model\MetaData->readMetaDataIndex(Object(Pages: 12), 0)
#3  Phalcon\Mvc\Model\MetaData->getAttributes(Object(Pages: 12))
#4  Phalcon\Mvc\Model->save(Array([username] => aa), Array([0] => username))


95

So, did u get the answer? I have this bug too. WTF? There's no google info about it ( What does it mean? How to fix this?

I got the same problem, for 2 reasons :

  • Rights of the user on the DB

    I used postgresql, and I didn't know that you have to grant permission for each table, and not use a 'TO dbname.*' like mysql.

  • Name of the table

    Because a table name in Merise and in Oriented Object don't have the same syntax, some differents may append between the model in phalcon and the relation in your database. If this is the case, juste add the following method in you model :

 public function getSource()
{
  return "real_table_name";
}

Like describe here http://docs.phalconphp.com/en/latest/reference/models.html#creating-models

Thanks, this fixed the problem for me.

I got the same problem, for 2 reasons :

  • Rights of the user on the DB

    I used postgresql, and I didn't know that you have to grant permission for each table, and not use a 'TO dbname.*' like mysql.

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