LoginSignup
0
2

More than 5 years have passed since last update.

CakePHP3でデータベース変更の後カラムが更新されない件について

Posted at

一回の処理で、一つのDBにデータを持ってきて、また、違うDBに保存する時
もし、この二つのテーブルの構造が若干違うのであれば、変に、前のテーブルのカラムが残されちゃって、

Column not found: 1054 Unknown colum `Articles.status`

このように、勝手に二つ目のDBのテーブルを操作しているのに、一つ目のDBのテーブルの構造で処理を続く
色々調べても出て来なかったので、CakePHPソースコードを読んで_processSave()find()$this->getSchema()->columns()というメソッドを呼び出していて、見ると、こんな感じです

Cake\ORM\Table.php
class Table implements RepositoryInterface, EventListenerInterface, EventDispatcherInterface, ValidatorAwareInterface
{
.
.
.
529行目
          /**
     * Returns the schema table object describing this table's properties.
     *
     * @return \Cake\Database\Schema\TableSchema
     */
    public function getSchema()
    {
        if ($this->_schema === null) {
            $this->_schema = $this->_initializeSchema(
                $this->getConnection()
                    ->getSchemaCollection()
                    ->describe($this->getTable())
            );
        }

        return $this->_schema;
    }

ここは、一回テーブルの処理によって、一回しか呼ばれないので、schemaはインスタンス化により、キャッシュされてしまうので
DBを変更するとき、こんな感じで書けば、レセットできますね

ArticlesController.php
    $connection = ConnectionManager::get('default');
    $this->Articles->setConnection($connection)->setSchema(null);

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2