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.

Generated SQL column names not correct

Hello,

I am trying to set up a many-to-many relationship for two models in my project. However, I get an error that the column userId does not exist. I have not told it to userId for the column name anywhere. If I paste the raw sql from mysql log into a database tool and change the column names, it works fine. Why is Phalcon using camelcase instead of underscore_case?

I have listed my three models with their defined relationships in initialize( ) below User Model

class User extends Model
{
public $id;
public $uuid;
public $signatureUuid;
public $firstName;
public $lastName;
public $loginEmail;
public $lastLogin;
public $accountStatus;
public $created;
public $modified;

public function columnMap()
{
    return array(
        'id'                => 'id',
        'uuid'              => 'uuid',
        'signature_uuid'    => 'signatureUuid',
        'login_email'       => 'loginEmail',
        'first_name'        => 'firstName',
        'last_name'         => 'lastName',
        'last_login'        => 'lastLogin',
        'account_status'    => 'accountStatus',
        'created'           => 'created',
        'modified'          => 'modified'
    );
}

/**
 * Initializes settings
 */
public function initialize()
{
    $this->hasManyToMany(
        'id',
        'Models\SaveForLaterItemModel',
        'user_id', 'item_id',
        'Models\ItemModel',
        'id',
        array('alias' => 'savedItems')
    );

    $this->skipAttributesOnUpdate(array(
        'id',
        'uuid',
        'created'
    ));
}
}

Item Model

class ItemModel extends Model
{
public $id;
public $uuid;
public $itemTypeId;
public $name;
public $description;
public $created;
public $modified;

public function columnMap()
{
    return array(
        'id'                        => 'id',
        'uuid'                      => 'uuid',
        'item_type_id'              => 'itemTypeId',
        'name'                      => 'name',
        'description'               => 'description',
        'created'                   => 'created',
        'modified'                  => 'modified'
    );
}

/**
 * Initializes settings
 */
public function initialize()
{
    $this->hasManyToMany(
        'id',
        'Models\SaveForLaterItemModel',
        'item_id', 'user_id',
        'Models\User',
        'id',
        array('alias' => 'users')
    );
}
}

Third model, linking the other two together

class SaveForLaterItemModel extends Model
{
public $item_id;
public $user_id;
public $created;
public $modified;

public function columnMap()
{
    return array(
        'item_id'                   => 'item_id',
        'user_id'                   => 'user_id',
        'created'                   => 'created',
        'modified'                  => 'modified'
    );
}
/**
 * Initializes settings
 */
public function initialize()
{
    $this->belongsTo('item_id', 'Models\ItemModel', 'id');
    $this->belongsTo('user_id', 'Models\User', 'id');

    $this->skipAttributesOnUpdate(array(
        'id',
        'created'
    ));
}
}

This is how I am using the relationship. I would like to find all items for a user:

$rows = User::findFirst()->getSavedItems();

Here is the resulting SQL from mysql.log:

40 Connect  [email protected] on foobar
40 Query    SELECT `users`.`id`, `users`.`uuid`, `users`.`signature_uuid`, `users`.`first_name`, `users`.`last_name`, `users`.`login_email`, `users`.`account_status`, `users`.`last_login`, `users`.`created`, `users`.`modified` FROM `users` LIMIT 1
40 Query    SELECT `items`.`id`, `items`.`uuid`, `items`.`item_type_id`, `items`.`name`, `items`.`description`, `items`.`created`, `items`.`modified` FROM `items` INNER JOIN `save_for_later_items` ON `save_for_later_items`.`itemId` = `items`.`id`  WHERE `save_for_later_items`.`userId` = '1'
40 Quit

Here is the error that I am receiving:

"Server Error","_Debug":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'save_for_later_items.userId' in 'where clause'"

If I change the column names itemId and userId to be item_id and user_id (like I would expect because a) that is how the column map is setup and b) that is how the table is defined), and run the query through a sql db tool, it works fine...

Why is Phalcon generating the incorrect column names?

Thank you



433
Accepted
answer

Turns out we are using redis to cache the models metadata and that was causing the old names of the model properties to stick around.

Solved with a redis cache flush.