0

私は他のテーブルとの(タイプDoctrine_Relation_AssociationDoctrine_Relation_ForeignKeyの)いくつかのオプションの関係を持つ教義テーブルを使用しています。そのテーブルのレコードが関連テーブルのレコードと接続しているかどうかをテストするにはどうすればよいですか。

これが私の質問をより明確にするための例です。ユーザーがいて、ユーザーがユーザーグループと多対多の関係を持ち、ユーザーが1つのユーザーロールを持つことができると仮定します。特定のユーザーがユーザーグループの一部であるか、ロールを持っているかをテストするにはどうすればよいですか。

解決策は私が信じているところから始まります

$relations = Doctrine_Core::getTable('User')->getRelations();
$user = Doctrine_Core::getTable('User')->findOne(1);
foreach($relations as $relation) {
    //here should go a test if the user has a related record for this relation
    if ($relation instanceof Doctrine_Relation_Association) {
       //here the related table probably has more then one foreign key (ex. user_id and group_id)    

    }
    if ($relation instanceof Doctrine_Relation_ForeignKey) {
        //here the related table probably has the primary key of this table (id) as a foreign key (user_id)
    }
}

//true or false
echo $result 

ユーザーと他のテーブルの間にいくつの関係があっても機能する一般的なソリューションを探しています。

ありがとう!

4

3 に答える 3

0

私はDoctrineを初めて使用しますが、これを試してください。

$relations = Doctrine_Core::getTable('User')->getRelations();
$user = Doctrine_Core::getTable('User')->findOne(1);
foreach($relations as $name => $relation) {
    //here should go a test if the user has a related record for this relation
    if($user->relatedExists($name)) {
       echo "this user is associated with some $name";
    }
}
于 2010-05-20T08:51:02.220 に答える
0
$user_id = // the user

$q = Doctrine_Query::create()
   ->select('ug.group_id')
   ->from('User u, u.UserGroup ug')
   ->where('u.user_id = ?', $user_id);
$results = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
foreach($results as $result) $relations[] = $result[0];
$relations = // simple array of group_ids the user belongs to

または、それが単に「はい/いいえ」の場合は、次のようにします。

$q = Doctrine_Query::create()
   ->select('COUNT(ug.group_id)')
   ->from('User u, u.UserGroup ug')
   ->where('u.user_id = ?', $user_id);
$results = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
$relation_exists = ($results[0][0]) ? true : false;
于 2010-05-20T12:50:44.807 に答える
0

これは古い質問ですが、他の誰かにとって、あるいはあなたにとっても役立つ可能性があります。あなたが探していたのは、relatedExists()ここで見つけることができる方法だったと思います:http: //docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/working-with-models.html#clearing-関連レコード

お役に立てば幸いです!

于 2013-03-30T15:11:24.487 に答える