Symfony varsayılan Propel kullanır ve bir eklenti olarak Doctrine destekler.
Table Bugs
Products
bir kavşak tablo ile tablo ile ilgili bir çok-çok ilişkisi aracılığıyla sorgulayarak Örnek 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>
Örnek sorgu:, bug # 1234 aramak çok-çok sorgusu ile ilgili ürünler almak ve rapor.
$bug = BugsPeer::retrieveByPK(1234);
$bugProducts = $bug->getBugsproductsJoinProducts();
foreach ($bugProducts as $bp) {
$product = $bp->getProducts();
print "bug id #".$bug->getBugId().": product ".$product->getProductName()."\n"
;
}
Solution using 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'));
}
}
Örnek sorgu:, bug # 1234 aramak çok-çok sorgusu ile ilgili ürünler almak ve rapor.
$bugsTable = Doctrine::getTable('Bugs');
$bug = $bugsTable->find(1234);
foreach ($bug->Products as $product) {
print 'Bug #'.$bug->bug_id.': product '.$product->product_name."\n";
}