Note that there are some explanatory texts on larger screens.

plurals
  1. POretrieving information from related tables with Zend Framework and Doctrine 1.2
    text
    copied!<p>After working hard in my ZF/Doctrine integration I'm having a problem "translating" my previous Zend_Db work into Doctrine. I used generate-models-db to create the models and I did got to access some properties form the view but only those concerning the table whose model I created like this:</p> <pre><code>$usuarios = new Model_Users(); $usr = $usuarios-&gt;getTable()-&gt;findAll(); $this-&gt;view-&gt;show = $usr; </code></pre> <p>Model_Users is related to two tables with this method:</p> <pre><code>public function setUp() { parent::setUp(); $this-&gt;hasMany('Model_PlanillaUsers as PlanillaUsers', array( 'local' =&gt; 'id', 'foreign' =&gt; 'users_id')); $this-&gt;hasMany('Model_UsersHasPais as UsersHasPais', array( 'local' =&gt; 'id', 'foreign' =&gt; 'users_id')); } </code></pre> <p>Right now I'm concerned about UsersHasPais...which tells me what pais.pais fields and which users.id entries match. This is the Model_Pais:</p> <pre><code>abstract class Model_Base_Pais extends Doctrine_Record { public function setTableDefinition() { $this-&gt;setTableName('pais'); $this-&gt;hasColumn('id', 'integer', 4, array( 'type' =&gt; 'integer', 'length' =&gt; 4, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; true, 'autoincrement' =&gt; true, )); $this-&gt;hasColumn('pais', 'string', 20, array( 'type' =&gt; 'string', 'length' =&gt; 20, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; true, 'autoincrement' =&gt; false, )); } public function setUp() { parent::setUp(); $this-&gt;hasMany('Model_UsersHasPais as UsersHasPais', array( 'local' =&gt; 'id', 'foreign' =&gt; 'pais_id')); } } </code></pre> <p>And this is the join table:</p> <pre><code>abstract class Model_Base_UsersHasPais extends Doctrine_Record { public function setTableDefinition() { $this-&gt;setTableName('users_has_pais'); $this-&gt;hasColumn('id', 'integer', 4, array( 'type' =&gt; 'integer', 'length' =&gt; 4, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; true, 'autoincrement' =&gt; true, )); $this-&gt;hasColumn('users_id', 'integer', 4, array( 'type' =&gt; 'integer', 'length' =&gt; 4, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; true, 'autoincrement' =&gt; false, )); $this-&gt;hasColumn('pais_id', 'integer', 4, array( 'type' =&gt; 'integer', 'length' =&gt; 4, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; true, 'autoincrement' =&gt; false, )); } public function setUp() { parent::setUp(); $this-&gt;hasOne('Model_Users as Users', array( 'local' =&gt; 'users_id', 'foreign' =&gt; 'id')); $this-&gt;hasOne('Model_Pais as Pais', array( 'local' =&gt; 'pais_id', 'foreign' =&gt; 'id')); } } </code></pre> <p>Now what I want to be able to retrieve,...if not clear enough is the fields called pais from the pais table that match with my current user id. How do I do this with Doctrine?</p> <p>EDIT:</p> <pre><code>//Added to Model_Users class public function saveUser($user) { $this-&gt;email = $user['email']; $this-&gt;password = crypt($user['password'], $this-&gt;_salt); $this-&gt;url = $user['url']; $this-&gt;responsable = $user['responsable']; $this-&gt;role = $user['role']; $this-&gt;fecha = Zend_Date::now()-&gt;toString('yyyyMMddHHmmss'); $id = $this-&gt;save(); } //Users table schema Users: connection: 0 tableName: users columns: id: type: integer(4) fixed: false unsigned: false primary: true autoincrement: true email: type: string(50) fixed: false unsigned: false primary: false notnull: false autoincrement: false password: type: string(250) fixed: false unsigned: false primary: false notnull: false autoincrement: false url: type: string(50) fixed: false unsigned: false primary: false notnull: false autoincrement: false responsable: type: string(50) fixed: false unsigned: false primary: false notnull: false autoincrement: false role: type: string(25) fixed: false unsigned: false primary: false notnull: false autoincrement: false fecha: type: timestamp(25) fixed: false unsigned: false primary: false notnull: true autoincrement: false relations: PlanillaUsers: local: id foreign: users_id type: many UsersHasPais: local: id foreign: users_id type: many </code></pre>
 

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