2011-06-02 43 views
1

在我的ZF/Doctrine集成中努力工作之後,我遇到了一個問題,即將我以前的Zend_Db工作「翻譯」爲Doctrine。我用生成的模型-DB創建模型,我也得到了訪問某些屬性形成的看法,但只有那些關於其模型我這樣創建的表:使用Zend Framework和Doctrine 1.2從相關表中檢索信息

$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。我如何與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

在控制器中編寫一個查詢類似

$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

@Graham我得到一個'未知關係別名Model_UsersHasPais'例外 – 2011-06-02 18:27:29

+0

@ la_f0ka我是新來的我自己的框架,檢查你的Apache錯誤日誌我已經看到它,但不記得是什麼導致它。我認爲這可能是一個缺失',或者從你的模型設置代碼中的一行,但我不是100%。 – Graham 2011-06-02 18:45:37

+0

@ la_f0ka或嘗試將查詢更改爲以下leftJoin - >('p.Model_UsersHasPais s') - > leftJoin(p.Model_Users u'),即在Model_Users之前刪除s,並用p – Graham 2011-06-02 18:54:31