バージョン : CakePHP 2.7.0
以下3つのモデルを使います。
・Post
・Tag
・PostsTag
PostとTagはそれぞれHABTMの関係になっています。
Tagで検索する処理を書きたいのですが、Postはbelongsto tagではないので、Tagをwhereに指定するとそんなカラムないと怒られます。
そのため、中間テーブルをPostとTagにbelongstoさせて、以下の通りにしていますが、TagとPostテーブルはjoinされません。
今まではpublic $belongsTo = array()でモデルを指定すればjoinしていたのですが。。。
もしお分りになる方がいらっしゃれば助けていただければと思います。
<PostsController>
public function tag($tag = null, $id = null) {
$this->loadModel('PostsTag');
$this->set('posts', $this->PostsTag->find('all',
array(
'conditions' => array('Tag.tag' => $tag),
'limit' => '30',
)
)
);
}
<PostsTag>
class PostsTag extends AppModel {
public $name = 'PostsTag';
public $belongsTo = array(
'Tag' => array(
'className' => 'Tag',
'foreignKey' => 'tag_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
ちなみにデバッグでPostsTagを見ると以下のようになります。
debug($this->PostsTag);
object(AppModel) {
useDbConfig => 'default'
useTable => 'posts_tags'
id => null
data => array()
schemaName => null
table => 'posts_tags'
primaryKey => 'id'
validate => array()
validationErrors => array()
validationDomain => null
plugin => null
name => 'PostsTag'
alias => 'PostsTag'
tableToModel => array(
'posts_tags' => 'PostsTag'
)
cacheQueries => false
belongsTo => array()
hasOne => array()
hasMany => array()
hasAndBelongsToMany => array()
actsAs => null
Behaviors => object(BehaviorCollection) {
modelName => 'PostsTag'
defaultPriority => (int) 10
[protected] _methods => array()
[protected] _mappedMethods => array()
[protected] _enabled => array()
[protected] _loaded => array()
}
whitelist => array()
cacheSources => true
findQueryType => null
recursive => (int) 1
order => null
virtualFields => array()
__backAssociation => array()
__backInnerAssociation => array()
__backOriginalAssociation => array()
__backContainableAssociation => array()
__safeUpdateMode => false
useConsistentAfterFind => true
findMethods => array(
'all' => true,
'first' => true,
'count' => true,
'neighbors' => true,
'list' => true,
'threaded' => true
)
[protected] _schema => null
[protected] _associationKeys => array(
'belongsTo' => array(
(int) 0 => 'className',
(int) 1 => 'foreignKey',
(int) 2 => 'conditions',
(int) 3 => 'fields',
(int) 4 => 'order',
(int) 5 => 'counterCache'
),
'hasOne' => array(
(int) 0 => 'className',
(int) 1 => 'foreignKey',
(int) 2 => 'conditions',
(int) 3 => 'fields',
(int) 4 => 'order',
(int) 5 => 'dependent'
),
'hasMany' => array(
(int) 0 => 'className',
(int) 1 => 'foreignKey',
(int) 2 => 'conditions',
(int) 3 => 'fields',
(int) 4 => 'order',
(int) 5 => 'limit',
(int) 6 => 'offset',
(int) 7 => 'dependent',
(int) 8 => 'exclusive',
(int) 9 => 'finderQuery',
(int) 10 => 'counterQuery'
),
'hasAndBelongsToMany' => array(
(int) 0 => 'className',
(int) 1 => 'joinTable',
(int) 2 => 'with',
(int) 3 => 'foreignKey',
(int) 4 => 'associationForeignKey',
(int) 5 => 'conditions',
(int) 6 => 'fields',
(int) 7 => 'order',
(int) 8 => 'limit',
(int) 9 => 'offset',
(int) 10 => 'unique',
(int) 11 => 'finderQuery'
)
)
[protected] _associations => array(
(int) 0 => 'belongsTo',
(int) 1 => 'hasOne',
(int) 2 => 'hasMany',
(int) 3 => 'hasAndBelongsToMany'
)
[protected] _insertID => null
[protected] _sourceConfigured => false
[protected] _eventManager => null
[protected] _validator => null
}
以下3つのモデルを使います。
・Post
・Tag
・PostsTag
PostとTagはそれぞれHABTMの関係になっています。
Tagで検索する処理を書きたいのですが、Postはbelongsto tagではないので、Tagをwhereに指定するとそんなカラムないと怒られます。
そのため、中間テーブルをPostとTagにbelongstoさせて、以下の通りにしていますが、TagとPostテーブルはjoinされません。
今まではpublic $belongsTo = array()でモデルを指定すればjoinしていたのですが。。。
もしお分りになる方がいらっしゃれば助けていただければと思います。
<PostsController>
public function tag($tag = null, $id = null) {
$this->loadModel('PostsTag');
$this->set('posts', $this->PostsTag->find('all',
array(
'conditions' => array('Tag.tag' => $tag),
'limit' => '30',
)
)
);
}
<PostsTag>
class PostsTag extends AppModel {
public $name = 'PostsTag';
public $belongsTo = array(
'Tag' => array(
'className' => 'Tag',
'foreignKey' => 'tag_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
ちなみにデバッグでPostsTagを見ると以下のようになります。
debug($this->PostsTag);
object(AppModel) {
useDbConfig => 'default'
useTable => 'posts_tags'
id => null
data => array()
schemaName => null
table => 'posts_tags'
primaryKey => 'id'
validate => array()
validationErrors => array()
validationDomain => null
plugin => null
name => 'PostsTag'
alias => 'PostsTag'
tableToModel => array(
'posts_tags' => 'PostsTag'
)
cacheQueries => false
belongsTo => array()
hasOne => array()
hasMany => array()
hasAndBelongsToMany => array()
actsAs => null
Behaviors => object(BehaviorCollection) {
modelName => 'PostsTag'
defaultPriority => (int) 10
[protected] _methods => array()
[protected] _mappedMethods => array()
[protected] _enabled => array()
[protected] _loaded => array()
}
whitelist => array()
cacheSources => true
findQueryType => null
recursive => (int) 1
order => null
virtualFields => array()
__backAssociation => array()
__backInnerAssociation => array()
__backOriginalAssociation => array()
__backContainableAssociation => array()
__safeUpdateMode => false
useConsistentAfterFind => true
findMethods => array(
'all' => true,
'first' => true,
'count' => true,
'neighbors' => true,
'list' => true,
'threaded' => true
)
[protected] _schema => null
[protected] _associationKeys => array(
'belongsTo' => array(
(int) 0 => 'className',
(int) 1 => 'foreignKey',
(int) 2 => 'conditions',
(int) 3 => 'fields',
(int) 4 => 'order',
(int) 5 => 'counterCache'
),
'hasOne' => array(
(int) 0 => 'className',
(int) 1 => 'foreignKey',
(int) 2 => 'conditions',
(int) 3 => 'fields',
(int) 4 => 'order',
(int) 5 => 'dependent'
),
'hasMany' => array(
(int) 0 => 'className',
(int) 1 => 'foreignKey',
(int) 2 => 'conditions',
(int) 3 => 'fields',
(int) 4 => 'order',
(int) 5 => 'limit',
(int) 6 => 'offset',
(int) 7 => 'dependent',
(int) 8 => 'exclusive',
(int) 9 => 'finderQuery',
(int) 10 => 'counterQuery'
),
'hasAndBelongsToMany' => array(
(int) 0 => 'className',
(int) 1 => 'joinTable',
(int) 2 => 'with',
(int) 3 => 'foreignKey',
(int) 4 => 'associationForeignKey',
(int) 5 => 'conditions',
(int) 6 => 'fields',
(int) 7 => 'order',
(int) 8 => 'limit',
(int) 9 => 'offset',
(int) 10 => 'unique',
(int) 11 => 'finderQuery'
)
)
[protected] _associations => array(
(int) 0 => 'belongsTo',
(int) 1 => 'hasOne',
(int) 2 => 'hasMany',
(int) 3 => 'hasAndBelongsToMany'
)
[protected] _insertID => null
[protected] _sourceConfigured => false
[protected] _eventManager => null
[protected] _validator => null
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
まだ回答がついていません
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.10%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる