0

I have this users table below.

enter image description here In phalcon, is it possible to map columnnames so that i can remove all the prefix names when executing all model related processes? In YII, this is doable. so lets say,

$user->id will point to $user->tbl_user_id;
$user->fullname will point to $user->tbl_user_fullname;
$user->phone will point to $user->tbl_user_phone;

I have no problem calling the table name since in the model you can specify the getSource(). I tried column mapping i found online but not working and not sure if its ideal.

Also, Will this cause possible issue if i'm going to implement it this way? Any known disadvantages?

PHP VERSION

PHP 5.4.44-0+deb7u1 (cli) (built: Aug 16 2015 09:51:53) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

user1149244
  • 711
  • 4
  • 10
  • 27
  • Why you need to have prefix for field names? (not just for table?)... I think u can achieve that with column mapping (u try this), but strange. I will research a little. – Boris Delev Sep 02 '16 at 09:39
  • That is how the table field names designed, that why i wanted to do table field or column mapping. – user1149244 Sep 02 '16 at 09:46

1 Answers1

3

You can map your database column names to your internal Phalcon naming:

Put this in your User model

public function columnMap()
{
    return [
        // database column name => phalcon name
        'tbl_user_id'       => 'id',
        'tbl_user_fullname' => 'fullname',
        'tbl_user_phone'    => 'phone',
    ];
}

Check the documentation for more information about the column mapping.

The ORM supports an independent column map, which allows the developer to use different column names in the model to the ones in the table. Phalcon will recognize the new column names and will rename them accordingly to match the respective columns in the database. [...].

serghei
  • 3,069
  • 2
  • 30
  • 48
Timothy
  • 2,004
  • 3
  • 23
  • 29
  • Hi Timothy, yes I've checked on that. Sorry, my question wasn't so clear. I want to know is, is it possible that i don't need to define it in all models? So that if there is tbl_user, tbl_bag, tbl_contacts all 3 models will just read one mapping files? – user1149244 Sep 03 '16 at 09:35
  • There is no build in way to do that, no. – Timothy Sep 03 '16 at 09:42