cakephpcakephp-3.xcakephp-bake

CakePHP 3 bake model with same table name from different datasource


I'm using CakePHP 3.

I have different sources to use and each having a different users table.

In short, I'm creating a master panel to manage users from all other applications.

When I bake users table from a different connection

bin/cake bake model users -c my_con

It asks to override earlier baked users model.

How can I bake table with the same name from the different source? maybe under a different namespace.

And it is impossible to get which index is representing which column.

How can I map the data with the column name of the table?


Solution

  • You could use a different model name, and explicitly pass the database name via the --table option, something like:

    bin/cake bake model UsersAlias --table users -c my_con
    

    Using a different namespace isn't possible directly, for models/tables Bake only supports creating files into plugins, that would be a different namespace, but you'd of course need a separate plugin for each source, which might be a little over the top.

    And fetchAll() has one argument that accepts the fetch type as a string, 'assoc', 'obj', or 'num' (default), also available as constants on \Cake\Database\Statement\PDOStatement:

    $result = $query->fetchAll(\Cake\Database\Statement\PDOStatement::FETCH_TYPE_ASSOC);