2010-08-11 53 views
2

我試圖使用飛行關聯來修剪我檢索的數據,但是我使用的模型與其他模型相關聯的重命名字段,因爲我有2個與它相關的相同模型。CakePhp:使用重新命名的模型字段實時關聯?

所以,這裏是模型,說'測試',有兩個'用戶'字段,都與用戶模型有關。

在模型:

var $belongsTo = array( 
    'User' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id' 
    ), 
    'User_Watched' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id_watched' 
    ) 
); 

當我取回與「測試」的數據,我只想找回鏈接到「用戶」和「User_Watched」域,但沒有任何其他嵌套的信息特定數據。

但是當我做:

$this->User->unbindModel(array('hasMany' => array('something1', 'something2')), false); 

然後something1和something2數據沒有顯示出來的模型「測試」的「用戶」的領域,但對於「User_watched」領域還在檢索。

我能否爲'User_watched'字段檢索不需要的數據?

希望這是有道理... :)

回答

2

使用的飛行協會修剪 下來的數據重新獲取

好主意。

'foreignKey' => 'user_id_watched' 

應該可能是:

'foreignKey' => 'user_watched_id'

編輯1:根據我目前的理解,至少這是有意義的。如果user_id是一個正確的外鍵(FK),哪個cakephp用來解除關係,但user_id_watched不是,比你描述的行爲有所解釋。

編輯2:可包含行爲爲您提供了另一個控制關聯模型的工具。

+0

謝謝,本傑明。爲什麼它必須以'id'結尾? 'Test'模型中的實際字段是'user_id_watched'。 我確實使用'連接'來解決這個問題。 – KcYxA 2010-08-12 21:13:36

+0

Hello KcYxA, Cakephp使用'Convention over Configuration'方法,這意味着如果開發人員堅持一些約定,則交換cakephp會減少所需的配置數量。 在這種情況下,'user_id_watched'充當FK。 cakephp中的FK應該遵循約定,所以表中的字段名應該是'user_watched_id'。那麼當然所有舊名稱的出現都必須相應地改變。 如果這對你有用,請讓我知道,以便我可以在上面的答案中解釋此評論。親愛的,本傑明。 – benjamin 2010-08-12 23:23:25

2

KcYxA,

中可容納的行爲可能有很大的幫助在這種情況下,如本傑明所說,你的「查找」查詢看起來像:

$this->User->find('first', array(
     'conditions' => array('User.id' => $id), 
     'contain' => array('UserWatched') 
    )); 

在這種情況下,你將不必使用unbindModel方法。在這個例子中,你會得到User和UserWatched數據。 如果你只需要來自「查找」的用戶數據,那麼告訴Cake「$ this-> User-> contains();」所以它不會走得更遠然後用戶模型。

+0

謝謝。我結束了使用連接。 – KcYxA 2010-08-12 21:14:12

+1

KcYxA, 使用「手寫」SQL應該是最後的手段,因爲它繞過了底層數據庫層的抽象。換句話說,直接引入SQL語句的開發人員在切換到另一個數據庫/數據源時必須重寫這些語句。親愛的,本傑明。 – benjamin 2010-08-13 00:04:33

+0

jujav4ik,它仍然使用CakePHP的慣例編寫,其中向'find'方法的參數添加了'join'數組。但我理解並同意你的觀點。 – KcYxA 2010-08-15 16:29:39

0

變化$的PrimaryKey在飛行,運行控制 樣品:

//模型

// ....

類PreProductoDescripcion延伸AppModel {

/** 
* Primary key field 
* 
* @var string 
*/ 
public $primaryKey = 'id_producto_descripcion'; 

//.... 
//.... 

}

類SenasaPedidosDetalles延伸AppModel {

/** 
* Display field 
* 
* @var string 
*/ 
public $displayField = 'cod_tango'; 

public $belongsTo = array(
    'SenasaPedidos' => array(
     'className' => 'SenasaPedidos', 
     'foreignKey' => 'senasa_pedidos_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ), 
    'PreProductoDescripcion' => array(
     'className' => 'PreProductoDescripcion', 
     'foreignKey' => 'cod_tango', 
     //'conditions' => array('SenasaPedidosDetalles.cod_tango' => 'PreProductoDescripcion.codigo'), 
     'fields' => '', 
     'order' => '' 
    ) 
); 

// ....

// Controller Fly 
//... 
$this->SenasaPedidos->Behaviors->load('Containable'); 
$this->SenasaPedidos->SenasaPedidosDetalles->PreProductoDescripcion->primaryKey = 'codigo'; 
        $datos = $this->SenasaPedidos->find(
          'first', array(
         'fields' => array('SenasaPedidos.*'), 
         'conditions' => array('SenasaPedidos.id' => $id), 
         'contain' => array(
          'Usuarios' => array(
           'fields' => array('Usuarios.apellido_nombre') 
          ), 
          'Clientes' => array(
           'fields' => array('Clientes.razon_social') 
          ), 
          'Provincias' => array(
           'fields' => array('Provincias.nombre') 
          ), 
          'Transportes' => array(
           'fields' => array('Transportes.razon_social') 
          ), 
          'SenasaPedidosDetalles' => array(
           'fields' => array('SenasaPedidosDetalles.*'), 
           'PreProductoDescripcion' => array(
            'fields' => array(
             'PreProductoDescripcion.id_producto_descripcion', 
             'PreProductoDescripcion.descripcion' 
            ) 
           ) 
          ), 
         ) 
        )); 
//...