K
K
Khurshed Abdujalil2017-03-23 15:41:06
Yii
Khurshed Abdujalil, 2017-03-23 15:41:06

How to add one record in two tables (different databases)?

There is a task when adding one record to the table {table} in the database {bd1} add to {bd2} in the same table
Copied model Item and named ItemExport and inherited from Item since the tables are the same (deleted all methods and added getDbConnection() )
then when saving in afterSave, Item will save in ItemExport
Also, ItemExport has this to connect to {bd2}

public function getDbConnection()
  {
    return Yii::app()->dbexport;
  }

and in {bd2} in the table {table} there is a field id_table_bd1
But when in AfterSave the Item does
$item = ItemExport::model()->findByAttributes(array('id_table_bd1' => $this->id));
gives an error that there is no such field,
he makes a selection in bd1,
what is wrong in my actions? Or is it better to do a simple and rough copy of the entire Item with all methods, then I think there will be no problems, but I don’t want to (because in my opinion it’s ugly)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LINKeR UA, 2017-03-23
@LINKeRxUA

master->slave replication or use 2 connections. and do execute queries on 2 connections

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question