バージョン : 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
}
あなたの回答
tips
プレビュー