Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony and Doctrine Templates - Setting up a one-to-many relationship
    primarykey
    data
    text
    <p>In my DB schema I have several tables that will be related to a parent table. The "ugly" way to solve the relation stuff would be to include the dependencies manually in the schema:</p> <pre><code>sfArea: columns: id: integer name: string sfCity: columns: name: string area_id: integer relations: Area: class: sfArea local: area_id foreignType: many foreignAlias: Cities sfItem: columns: name: string area_id: integer relations: Area: class: sfArea local: area_id foreignType: many foreignAlias: Items </code></pre> <p>However, each time that I will add a class to be attached to the area, I will need to add the relation and all the lines that go with it (copy/paste => future hell). This is where I decided to use the Doctrine_Template, which allows me to achieve the same thing like that:</p> <pre><code>sfArea: columns: id: integer name: string sfCity: actAs: AreaRelated: { foreignAlias: Cities } columns: name: string sfItem: actAs: AreaRelated: { foreignAlias: Items } columns: name: string </code></pre> <p>And the template class:</p> <pre><code>class AreaRelated extends Doctrine_Template { protected $_options = array( 'foreignAlias' =&gt; '' ); public function setTableDefinition() { $this-&gt;hasColumn('area_id', 'integer'); } public function setUp() { $this-&gt;hasOne('sfArea as Area', array( 'local' =&gt; 'area_id', 'foreign' =&gt; 'id', 'foreignType' =&gt; 'many', 'foreignAlias' =&gt; $this-&gt;_options['foreignAlias'] ) ); } } </code></pre> <p>The tables are generated properly and the relation works in the direction $sfCity->Area. However, the relations that should be set up in the sfArea class are not created ($sf_area->Cities give the error "Unknown record property / related component "Cities" on "sfArea""). </p> <p>How can the other relation be created? I even tried that (without success):</p> <pre><code>//... public function setUp() { $thisTable = $this-&gt;_table; $areaTable = Doctrine::getTable("smArea"); $thisTable-&gt;hasOne('smArea as Area', array( 'local' =&gt; 'area_id', 'foreign' =&gt; 'id', 'foreignType' =&gt; Doctrine_Relation::MANY ) ); $areaTable-&gt;hasMany($thisTable-&gt;getOption('name') . ' as ' . $this-&gt;_options['foreignAlias'], array( 'local' =&gt; 'id', 'foreign' =&gt; 'area_id', 'foreignType' =&gt; Doctrine_Relation::ONE ) ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload