使用Zend Framework和Doctrine 1.2从相关表中检索信息

时间:2011-06-02 15:15:47

标签: zend-framework doctrine

在我的ZF / Doctrine集成中努力工作之后,我遇到了将我之前的Zend_Db工作“翻译”为Doctrine的问题。我使用generate-models-db来创建模型,我确实从视图中访问了一些属性,但只有那些关于我创建的模型的表格如下:

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

Model_Users与使用此方法的两个表相关:

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'));
}

现在我关注UsersHasPais ...它告诉我什么是pais.pais字段和哪些users.id条目匹配。这是Model_Pais:

abstract class Model_Base_Pais extends Doctrine_Record
{
public function setTableDefinition()
{
    $this->setTableName('pais');
    $this->hasColumn('id', 'integer', 4, array(
         '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'));
}
}

这是联接表:

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'));
}
}

现在我希望能够检索,...如果不够清楚,那就是pais表中与我当前用户ID匹配的pais字段。我如何使用Doctrine进行此操作?

编辑:

//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

1 个答案:

答案 0 :(得分:1)

在您的控制器中编写类似

的查询
  $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();