Yii Framework Forum: Getting all foreign keys of the table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Getting all foreign keys of the table Rate Topic: -----

#1 User is offline   panzer_commander 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-January 15

Posted 17 January 2015 - 04:16 AM

Hello, I trying to get all foreign keys of the specify table, but when I do this:
        $sc = new CMssqlTableSchema();
        $sc->name = 'tbl_users';
        var_dump($sc->foreignKeys);die;

it display
array (size=0)
  empty

What I am doing wrong? :)
0

#2 User is offline   phtamas 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 762
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 17 January 2015 - 05:15 AM

CDbTableSchema and its subclasses are simple data holders. They cannot connect to database and fetch metadata. Try this instead:
$sc = Yii::app()->db->schema->getTable('tbl_users'); // returns a CMssqlTableSchema instance populated with table data

0

#3 User is offline   panzer_commander 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-January 15

Posted 17 January 2015 - 05:40 AM

View Postphtamas, on 17 January 2015 - 05:15 AM, said:

CDbTableSchema and its subclasses are simple data holders. They cannot connect to database and fetch metadata. Try this instead:
$sc = Yii::app()->db->schema->getTable('tbl_users'); // returns a CMssqlTableSchema instance populated with table data



var_dump of this equal to null :)
0

#4 User is offline   phtamas 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 762
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 17 January 2015 - 07:19 AM

View Postpanzer_commander, on 17 January 2015 - 05:40 AM, said:

var_dump of this equal to null :)


It means that the table doesn't exist. Some possible reasons:
- Table tbl_users doesn't actually exist.
- You have multiple databases associated with the application and Yi::app()->db connects to the wrong one. In this case use the appropriate connection.
- I'm not familiar enough with MSSQL, but can it be a case-sensitivity issue?
0

#5 User is offline   panzer_commander 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-January 15

Posted 17 January 2015 - 08:23 AM

View Postphtamas, on 17 January 2015 - 07:19 AM, said:

It means that the table doesn't exist. Some possible reasons:
- Table tbl_users doesn't actually exist.
- You have multiple databases associated with the application and Yi::app()->db connects to the wrong one. In this case use the appropriate connection.
- I'm not familiar enough with MSSQL, but can it be a case-sensitivity issue?


Table exists in database, I even connected to database manualy, and tried to find table many ways because it have tabl prefix. Still null. I used code like:
$ccon = new CDbConnection('mysql:host=localhost;dbname=chooseone', 'root', '*******');
$ccon->schema->getTable('tbl_users'); // still null
$ccon->schema->getTable('users'); // null
$ccon->schema->getTable('{{users}}'); // null

0

#6 User is offline   Argent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 23-February 13
  • Location:Hungary

Posted 17 January 2015 - 04:15 PM

What you get if you try to print the table names?
$ccon = new CDbConnection('mysql:host=localhost;dbname=chooseone', 'root', '*******');
$tableNames = $ccon->schema->getTableNames();
var_export($tableNames);

0

#7 User is offline   panzer_commander 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-January 15

Posted 19 January 2015 - 02:50 AM

View PostArgent, on 17 January 2015 - 04:15 PM, said:

What you get if you try to print the table names?
$ccon = new CDbConnection('mysql:host=localhost;dbname=chooseone', 'root', '*******');
$tableNames = $ccon->schema->getTableNames();
var_export($tableNames);


I try to get names of foreign keys to drop them by
CDbCommand -> dropForeignKey()
, but when I getting FK's, them represented as array, there are missing name of fk, only fields and related table.
0

#8 User is offline   phtamas 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 762
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 20 January 2015 - 11:16 AM

View Postpanzer_commander, on 19 January 2015 - 02:50 AM, said:

I try to get names of foreign keys to drop them by
CDbCommand -> dropForeignKey()
, but when I getting FK's, them represented as array, there are missing name of fk, only fields and related table.

I don't think Yii has built in support for it. If you really need this level of runtime database introspection you might want to try Doctrine DBAL. Its schema manager can provide a bit more detailed information about your database structure.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users