メソッド | 関連するデータを取得するか? | 関連するデータに対して条件を設定し、元のテーブルを絞り込めるか? |
---|---|---|
contain | ○ | ☓ |
matching | ○ | ○ |
innerJoinWith | ☓ | ○ |
注意事項:containとmatchingは排他利用ではない
メソッド | 関連するデータを取得するか? | 関連するデータに対して条件を設定し、元のテーブルを絞り込めるか? |
---|---|---|
contain | ○ | ☓ |
matching | ○ | ○ |
innerJoinWith | ☓ | ○ |
注意事項:containとmatchingは排他利用ではない
とても便利なbakeですが、hasOneのリレーションを設定してくれないという特徴があります。
例えば、次のようなテーブルがあるとします: usersテーブル
user_profilesテーブル
Users hasMany UserProfilesというモデルが生成されます。
UsersTable.php
public function initialize(array $config) { parent::initialize($config); $this->setTable('users'); $this->setDisplayField('id'); $this->setPrimaryKey('id'); $this->hasMany('UserProfiles', [ 'foreignKey' => 'user_id' ]); }
UserProfilesTable.php
public function initialize(array $config) { parent::initialize($config); $this->setTable('user_profiles'); $this->setDisplayField('id'); $this->setPrimaryKey('id'); $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER' ]); }
なお、この挙動はuser_profiles.user_idカラムにユニークキーを貼っても変わりありません。
コントローラやテンプレートをbakeした際に、hasMany前提でbakeされてしまいます。
フォームで関連するデータの保存を行う場合に、添字の指定が必要になります:
$this->Form->control('email'); $this->Form->control('user_profile.0.first_name'); // hasOneでbakeしていればuser_profile.first_nameと書ける $this->Form->control('user_profile.0.last_name');
bake all
をすると、hasOneにしたいのにhasManyになり、hasMany前提でコントローラとテンプレートが生成されてしまいます。
よって、次のような手順が良さそうです:
$data = $this->request->getData(); $data['order_data']['name'] = "新しい名前"; $order = $this->Orders->patchEntity($order, $data);
$order->order_data->name = "新しい名前"; $order->dirty('order_data',true); $this->Orders->save($order);
2行目のdirtyの指定を忘れると、保存されません
debugkitを入れていればSQLは確認できますが、リダイレクト等が入っていて確認できない場合があります。 その対策です。
bootstrap.php
にこれを追加します:
Log::config('queries', [ 'className' => 'File', 'path' => LOGS, 'file' => 'queries.log', 'scopes' => ['queriesLog'] ]);
デバッグモードを無効にします(なぜこれをやらねばならないのか、理解に苦しみますが…)
コントローラで:
use Cake\Datasource\ConnectionManager;
を追加し、かつ、ログしたいクエリの周りに:
$connection = ConnectionManager::get('default'); $connection->logQueries(true); $this->Orders->save($order); $connection->logQueries(false);
といった具合にlogQueriesを入れます。
なお、app.php
のDatasourcesの中のlog
はfalse
のままでOKです。
UPDATE前と後の値に変更がない場合、UPDATEは発行されないようです(したがってログにも出ない)
install -d -m 700 ~/.ssh (umask 077; touch ~/.ssh/authorized_keys)
これで正しいパーミッションで作成できます。
git checkout -b 新しいブランチ名
git branch
git branch -d ブランチ名
git branch -D ブランチ名
git branch --remote
git push origin --delete ブランチ名