CakePHP 1.3.4 $は、関係テーブルの場合にbelunkstoの問題
-
25-09-2019 - |
質問
user_relationshipというテーブルがあります。ユーザーテーブルに戻って友達であることをマッピングする2つの外国の鍵があります。
CREATE TABLE `user_relationships` (
`id` int(11) unsigned NOT NULL auto_increment,
`status` varchar(255) default 'pending',
`time` datetime default NULL,
`user_id` int(11) unsigned NOT NULL,
`friend_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_relationships_users1` (`user_id`),
KEY `fk_user_relationships_users2` (`friend_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
私がベイクしようとすると、それを自然にベイクしようとすると、friend_idがユーザーモジュールを参照する必要があることを理解していません。コードを手動で編集したいのですが、o以下で編集するパーツを理解するのに問題があります
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'friend_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
コードのこの部分では、friend_idをユーザーテーブルに紹介したい
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'friend_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
私はこれをやってみました..私は他に何かを変える必要がありますか?
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
解決
これはどう?
class UserRelationship {
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Friend' => array(
'className' => 'User',
'foreignKey' => 'friend_id'
)
);
}
class User {
var $hasAndBelongsToMany = array(
'Friend' => array(
'className' => 'User',
'joinTable' => 'user_relationships',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
'with' => 'UserRelationship'
)
);
}
関連性は、さまざまな観点から見ることができます。
User <- Relationship -> User
| | |
hasMany belongsTo |
Relationship <- User (user_id) hasMany
| User (friend_id) -> Relationship
| |
HABTM HABTM
User <----------------------> User
あなたの「物理」レイアウト、すなわちデータベーススキーマは User -> Relationship -> User
. 。あなたの本当の望ましい関係はです User -> User
しかし、関係。技術的には、それはaに変換されます User hasAndBelongsToMany User
協会(多くの人と呼ばれる)。 3つのモデルを使用しているため、多くの関連性を分割できます。
- ユーザーHasmany Relationship/Relationship bulndstoユーザー
- 関係は、ユーザー/ユーザーの関係者の関係に属します
あなたはそうしない 必要 Hasandbelongstomany協会ですが、それはあなたが実際に望んでいるものです。 2人のユーザーを接続するのは単なるヘルパーモデルであるため、関係モデルはまったく必要ありませんが、 行う 協会も指定したいと考えています。2つの属性協会があります。
他のヒント
「友達」のクラス名は間違っています。テーブルの「友達」がないので、モデル「友達」はありません。同じテーブルを共有しているため、クラス「ユーザー」を参照しています。
'Friend' => array(
'className' => 'User',
'foreignKey' => 'friend_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
私は次のことを逃れることができました:
class User extends AppModel {
var $hasAndBelongsToMany = array(
'Friends1' => array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
'with' => 'Friend'
),
'Friends2' => array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'friend_id',
'associationForeignKey' => 'user_id',
'with' => 'Friend'
)
);
function afterFind(array $results) {
// Merge both sides of the relationship into one alias
foreach ($results as $key=>$result) {
if (isset($result["Friends1"]) && isset($result["Friends2"])) {
$results[$key]["Friends"] = array_merge($result["Friends1"], $result["Friends2"]);
unset($results[$key]["Friends1"]);
unset($results[$key]["Friends2"]);
}
}
それは乱雑ですが、あなたはアイデアを得ます:関係の両側をつかんでから、データベースを読んだ後にそれらをマージします:)