Many-to-many search

I am making a waitlist application. I would like to be able to send applications a unique verification code - and later I will be able to find the user either using the verification code or by user name.

Will Symfony be able to invoke models to say:

Code->findUser($code_string);

User->getCode();

I believe the above schema has relationships, but I'm not sure if this is Symfony's way of linking these relationships.

Thank you for your time,

  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:
+3
source share
1 answer

Symfony uses Propel by default and supports Doctrine as a plugin.

An example of a query through a many-to-many relationship, where the table Bugsis linked to the table Productsthrough the intersection table BugsProducts:

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

Solution using Propel:

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'));
  }
}

: # 1234, " " .

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

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

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

All Articles