tags:

views:

23

answers:

2

There are 3 tables:

catalog
product
product2catalog

Table product2catalog consists of the following fields:

catalog_id
product_id

How to get goods within the directory? How to rebuild the connection to be able to do so:

$CatalogTable = CatalogTable::getInstance();
$Catalog = $CatalogTable->find( $id );
var_dump( $Catalog->product );

Could you help me?

+1  A: 

Many to many relationships are easy in Doctrine, as outlined here.

Set up your relationship like this: (in the catalog table's setUp() function)

$this->hasMany('Product as Products',
array('local' => 'catalog_id',
      'foreign' => 'product_id',
      'refClass' => 'product2catalog'
    ));

Similarly, in YAML:

Catalog:
# ...
  relations:
   # ...
    Products:
      class: Product
      local: catalog_id
      foreign: product_id
      refClass: product2catalog

You have to also do the same for the product class - exact same syntax, just invert the local/foreign references.

Tyson
+1  A: 

To query a Many-to-Many relationship, you can do something like this:

$q = Doctrine_Query::create()
    ->from('Catalogue c')
    ->leftJoin('c.Product p')
    ->where('c.id = ?', $id);

$c = $q->fetchArray();
Jon