2008-11-18 123 views
3

我正在做一个等候名单申请。我希望能够向应用程序发送一个唯一的确认代码 - 并且稍后可以通过确认代码或用户名来查找用户。多对多查询

会的Symfony能够调用模型说两种:

Code->findUser($code_string); 

User->getCode(); 

我相信下面的模式有关系,但我不知道这是否是这些捆绑在一起的关系的方式Symfony的。

谢谢您的时间,

user: 
    id: 
    last_name: varchar(255) 
    first_name: varchar(255) 
    email: varchar(255) 

    code: 
    id: 
    secret: varchar(255) 

    user_code: 
    id: 
    user_id: 
    code_id: 

    course: 
    id: 
    title: varchar(255) 

    quarter: 
    id: 
    title: varchar(255) 

    wait_list: 
    id: 
    user_id: 
    course_id: 
    quarter_id: 

回答

2

Symfony的使用行走在默认情况下,支持主义作为一个插件。

实施例通过查询一个多到多的关系,其中表Bugs通过交叉路口表BugsProducts有关表Products

[Bugs] <-- [BugsProducts] --> [Products] 

解决方案使用行走:

schema.xml

<table name="Bugs"> 
    <column name="bug_id" type="INTEGER" required="true" 
    primaryKey="true" autoIncrement="true" /> 
    </table> 

    <table name="Products"> 
    <column name="product_id" type="INTEGER" required="true" 
    primaryKey="true" autoIncrement="true" /> 
    <column name="product_name" type="VARCHAR" size="50" required="true" /> 
    </table> 

    <table name="BugsProducts"> 
    <column name="bug_id" type="INTEGER" required="true" primaryKey="true" /> 
    <column name="product_id" type="INTEGER" required="true" primaryKey="true" /> 
    <foreign-key foreignTable="Bugs"> 
     <reference local="bug_id" foreign="bug_id" /> 
    </foreign-key> 
    <foreign-key foreignTable="Products"> 
     <reference local="product_id" foreign="product_id" /> 
    </foreign-key> 
    </table> 

示例查询:查找错误#1234,通过多对多查询获取相关产品,并进行报告。

$bug = BugsPeer::retrieveByPK(1234); 

$bugProducts = $bug->getBugsproductsJoinProducts(); 

foreach ($bugProducts as $bp) { 
    $product = $bp->getProducts(); 
    print "bug id #".$bug->getBugId().": product ".$product->getProductName()."\n" 
; 
} 

解决方案使用Doctrine:

class Bugs extends Doctrine_Record 
{ 
    public function setUp() 
    { 
    $this->hasMany('Products', array('local'=>'bug_id', 
       'foreign'=>'bug_id', 
       'refClass'=>'BugsProducts')); 
    } 
} 

class Products extends Doctrine_Record 
{ 
    public function setUp() 
    { 
    $this->hasMany('Bugs', array('local'=>'product_id', 
       'foreign'=>'product_id', 
       'refClass'=>'BugsProducts')); 
    } 
} 

例子查询:查找BUG#1234,经过许多一对多查询,报告获得相关产品。

$bugsTable = Doctrine::getTable('Bugs'); 

$bug = $bugsTable->find(1234); 

foreach ($bug->Products as $product) { 
    print 'Bug #'.$bug->bug_id.': product '.$product->product_name."\n"; 
}