2012-07-19 20 views
5

मैं एक साइट CakePHP 2.0 में विकसित की है, मैं दो तालिकाओं के बीच एक रिश्ता बनाना चाहते हैं:CakePHP विदेशी कुंजी नहीं प्राथमिक कुंजी

activity_ingredients

1 id int(10) UNSIGNED No None AUTO_INCREMENT 
2 type_id  tinyint(2) No None   
3 activity_id  int(11)  No None   
4 ingredient_id int(10)  No None   
5 created  datetime   

कार्यों

1 id int(10) UNSIGNED No None AUTO_INCREMENT 
2 type_id  tinyint(2) No None   
3 language char(2)  No None   
4 text varchar(100)  No None   
5 created  datetime  

मैं "table_id" फ़ील्ड के साथ दो तालिकाओं को जोड़ना चाहते हैं। मैं अपने कोड में इस मोड में किया है:

class Action extends AppModel{ 
    public $name = 'Action'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità 

    public $belongsTo = array(
     'ActivityIngredients' => array(
      'className'  => 'ActivityIngredients', 
      'conditions' => '', 
      'order'   => '', 
      'foreignKey' => 'type_id' 
     ) 
    ); 

}

class ActivityIngredients extends AppModel{ 
     public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità 

     public $belongsTo = array(
      'Activity' => array(
       'className'  => 'Activity', 
       'conditions' => '', 
       'order'   => '', 
       'foreignKey' => 'activity_id' 
      ), 
      'Ingredient' => array(
       'className'  => 'Ingredient', 
       'conditions' => '', 
       'order'   => '', 
       'foreignKey' => 'ingredient_id' 
      ) 
     ); 

     public $hasMany = array(
      'Action' => array(
       'className' => 'Action', 
       'conditions' => '', 
       'dependent' => true, 
       'foreignKey' => 'type_id', 
       'associatedKey' => 'type_id' 
      ) 
     ); 
    } 

यह सही डेटा प्राप्त नहीं करता है .. ऐसा लगता है कि यह विदेशी कुंजी के लिए आईडी लेता है।

<?php foreach ($user['Activity'] as $activities) { 
var_dump($activities); 
?> 
    <div class="line-cnt"><div class="line"> 
    </div> 
</div> 
<h2>Attività</h2> 
<div class="table"> 
    <div> 
     <div>Activity created</div><div><?php echo $activities['created']; ?> 
     </div> 
    </div> 
    <div> 
     <div>Actions text</div><div><?php echo $activities['Action']['text']; ?></div> 
    </div> 
    <div> 
     <div>ActivityIngredient ingredient_id</div><div><?php echo $activities['ActivityIngredients']['ingredient_id']; ?></div> 
    </div> 
</div> 
<?php 
} 
?> 

नियंत्रक उपयोगकर्ता कि टेबल के साथ collegate है में सभी और पुनरावर्ती 3 खोजने के साथ

$this->User->recursive = 3; 
     $user = $this->User->read(); 

     if (empty($username) || $username != $user['User']['username']) { 
      $this->redirect(array ('action'=>'view',$id,$user['User']['username'])); 
     } 

     $this->set('user', $user); 

मेरी मदद कृपया

उत्तर

2

पहली बात यह है कि अगर आप अपने "activity_ingredients" तालिका में एक "आईडी" क्षेत्र का उपयोग कर रहे हैं, तो आप उसे किसी अन्य तालिका में एक foreignKey के रूप में इस्तेमाल करना चाहिए।

एक विदेशी कुंजी एक संबंधित तालिका में एक फ़ील्ड है जो किसी अन्य तालिका की उम्मीदवार कुंजी से मेल खाती है।

भले ही आप type_id "कार्रवाई" तालिका में विदेशी कुंजी के रूप में उपयोग करने के लिए कोशिश कर रहे हैं, तो फिर type_id अपने activity_ingredients तालिका में अद्वितीय होना चाहिए, और अगर यह इतना है, तो आप के रूप में अपने ActivityIngredient मॉडल को परिभाषित कर सकते हैं:

class ActivityIngredients extends AppModel{ 
    public $primaryKey = 'type_id'; 
    public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità 

    public $belongsTo = array(
     'Activity' => array(
      'className'  => 'Activity', 
      'conditions' => '', 
      'order'   => '', 
      'foreignKey' => 'activity_id' 
     ), 
     'Ingredient' => array(
      'className'  => 'Ingredient', 
      'conditions' => '', 
      'order'   => '', 
      'foreignKey' => 'ingredient_id' 
     ) 
    ); 

    public $hasMany = array(
     'Action' => array(
      'className' => 'Action', 
      'conditions' => '', 
      'dependent' => true, 
      'foreignKey' => 'type_id', 
      'associatedKey' => 'type_id' 
     ) 
    ); 
} 

और आपका एक्शन मॉडल वही रहेगा। और इसलिए आप वांछित रिकॉर्ड लाने में सक्षम होंगे।

और यहां तक ​​कि यदि आप अपनी तालिका में "type_id" को विदेशी कुंजी के रूप में परिभाषित करने के लिए सहमत नहीं हैं। फिर यह कोड आपकी स्थिति के साथ बेहद अच्छी तरह से काम करेगा।

class ActivityIngredients extends AppModel{ 
public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità 

public $belongsTo = array(
    'Activity' => array(
     'className'  => 'Activity', 
     'conditions' => '', 
     'order'   => '', 
     'foreignKey' => 'activity_id' 
    ), 
    'Ingredient' => array(
     'className'  => 'Ingredient', 
     'conditions' => '', 
     'order'   => '', 
     'foreignKey' => 'ingredient_id' 
    ) 
); 

public $hasMany = array(
    'Action' => array(
     'className' => 'Action', 
     'conditions' => '', 
     'dependent' => true, 
     'foreignKey' => false, 
     'finderQuery' => 'select * from actions as `Action` where 
          `Action`.`type_id` = {$__cakeID__$} ' 
    ) 
); 

}

मुझे यकीन है कि यह आप इच्छित परिणाम दे देंगे कर रहा हूँ। कृपया पूछें कि क्या यह आपके लिए काम नहीं करता है।

-1

CakePHP में एक सरल प्रश्न है: यह दृश्य है आप primary key of a model असाइन कर सकते हैं। मेरा मानना ​​है कि आप क्लासनाम को विदेशी कुंजी एसोसिएशन में भी डाल सकते हैं। उदाहरण

'foreignKey' => 'Classname.type_id' 
संबंधित मुद्दे