开发者

retrieving information from related tables with Zend Framework and Doctrine 1.2

开发者 https://www.devze.com 2023-03-10 10:41 出处:网络
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 so

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:

$usuarios = new Model_Users();
$usr = $usuarios->getTable()->findAll();
$this->view->show = $usr;

Model_Users is related to two tables with this method:

public function setUp()
{
    parent::setUp();
    $this->hasMany('Model_PlanillaUsers as PlanillaUsers', array(
         'local' => 'id',
         'foreign' => 'users_id'));

    $this->hasMany('Model_UsersHasPais as UsersHasPais', array(
         'local' => 'id',
         'foreign' => 'users_id'));
}

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:

abstract class Model_Base_Pais extends Doctrine_Record
{
public function setTableDefinition()
{
    $this->setTableName('pais');
    $this->hasColumn('id', 'integer', 4, ar开发者_JAVA技巧ray(
         'type' => 'integer',
         'length' => 4,
         'fixed' => false,
         'unsigned' => false,
         'primary' => true,
         'autoincrement' => true,
         ));
    $this->hasColumn('pais', 'string', 20, array(
         'type' => 'string',
         'length' => 20,
         'fixed' => false,
         'unsigned' => false,
         'primary' => false,
         'notnull' => true,
         'autoincrement' => false,
         ));
}

public function setUp()
{
    parent::setUp();
    $this->hasMany('Model_UsersHasPais as UsersHasPais', array(
         'local' => 'id',
         'foreign' => 'pais_id'));
}
}

And this is the join table:

abstract class Model_Base_UsersHasPais extends Doctrine_Record
{
public function setTableDefinition()
{
    $this->setTableName('users_has_pais');
    $this->hasColumn('id', 'integer', 4, array(
         'type' => 'integer',
         'length' => 4,
         'fixed' => false,
         'unsigned' => false,
         'primary' => true,
         'autoincrement' => true,
         ));
    $this->hasColumn('users_id', 'integer', 4, array(
         'type' => 'integer',
         'length' => 4,
         'fixed' => false,
         'unsigned' => false,
         'primary' => false,
         'notnull' => true,
         'autoincrement' => false,
         ));
    $this->hasColumn('pais_id', 'integer', 4, array(
         'type' => 'integer',
         'length' => 4,
         'fixed' => false,
         'unsigned' => false,
         'primary' => false,
         'notnull' => true,
         'autoincrement' => false,
         ));
}

public function setUp()
{
    parent::setUp();
    $this->hasOne('Model_Users as Users', array(
         'local' => 'users_id',
         'foreign' => 'id'));

    $this->hasOne('Model_Pais as Pais', array(
         'local' => 'pais_id',
         'foreign' => 'id'));
}
}

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?

EDIT:

//Added to Model_Users class


public function saveUser($user) {
    $this->email = $user['email'];
    $this->password = crypt($user['password'], $this->_salt);
    $this->url = $user['url'];
    $this->responsable = $user['responsable'];
    $this->role = $user['role'];
    $this->fecha = Zend_Date::now()->toString('yyyyMMddHHmmss');
    $id = $this->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


In your controller write a query something like

  $cu = current_user_id // you'll have to set this your self from a session variable etc
  $q = Doctrine_Query::create()
       ->select('p.pais')   
       ->from('Model_Pais p')
       ->leftJoin('p.Model_UsersHasPais s')
       ->leftJoin('s.Model_Users u')
        ->where('u.id = ?',$cu); 
    $result = $q->fetchArray();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号