2013-07-04 17 views
13

में आईडी के बिना एलोक्वेंट के साथ डेटा कैसे अपडेट कर सकता हूं जब मैं FaqTrans डेटाबेस अपडेट करना चाहता हूं, लेकिन इस डेटास में दो प्राथमिक कुंजी (faq_ida और lang) $table->primary(array('lang', 'faq_id')); हैं। इसलिए मुझे auto_increment के साथ id कॉलम की आवश्यकता नहीं है।मैं लैरवेल 4

हालांकि, जब मैं डेटाबेस को अद्यतन करने के लिए निम्न कोड का उपयोग करता हूं, तो त्रुटि संदेश मुझे संकेत देता है कि id कॉलम नहीं है।

$faq_trans = FaqTrans::where('faq_id','=',$faq_id)->where('lang','=',$lang)->first(); 
$faq_trans->lang = Input::get('lang'); 
$faq_trans->body = Input::get('body'); 
$faq_trans->title = Input::get('title'); 
$faq_trans->save(); 

त्रुटि संदेश

SQLSTATE [42S22]: कॉलम नहीं मिला: 1054 अज्ञात स्तंभ 'आईडी' में 'जहां खंड' (एसक्यूएल: अद्यतन FAQtrans सेट body =, updated_at = जहां? id रिक्त है) (बाइंडिंग: सरणी (0 => 'adfadaaadfadaa', 1 => '2013-07-04 11:12:42',))

और जब मैं एक अतिरिक्त id कॉलम, कोड ठीक काम करता है ...

क्या मैं आईडी कॉलम के बिना डेटाबेस को अपडेट कर सकता हूं?

+0

आप अपने मॉडल –

+0

में प्राथमिक कुंजी मैं मॉडल में प्राथमिक कुंजी सेट करने की कोशिश की स्थापित किया था मदद करता है, लेकिन यह प्रतीत एक बार (समग्र कुंजी) में दो प्राथमिक कुंजी सेट नहीं कर सकते, जैसे 'संरक्षित $ primaryKey = array ('lang''faq_id'); ' –

+0

आपको सहेजने के बजाय' -> अद्यतन (सरणी ('कॉलम' => 'मान')) का उपयोग करना पड़ सकता है। –

उत्तर

7

लैरवेल/एलोक्वेंट समग्र प्राथमिक कुंजी का समर्थन नहीं करता है।

जब आप एलोक्वेंट मॉडल क्लास की जांच करते हैं, तो आप देख सकते हैं कि प्राथमिक कुंजी केवल एक स्ट्रिंग हो सकती है, जिसका उपयोग WHERE क्लॉज बनाने के लिए किया जाता है।

1

अपने FaqTrans मॉडल पर जाएं और इसे जोड़ें।

public $key = 'faq_id'; 

कि यह करना चाहिए, faq_id संभालने lang

+0

मुझे लगता है कि यह अब $ प्राथमिक है: http://laravel.com/डॉक्स/eloquent # मूल उपयोग – ftrotter

27

लिखें अपने मॉडल में इस लाइन से ज्यादा महत्वपूर्ण है

public $primaryKey = '_id';

_ id कहाँ अपने मैनुअल प्राथमिक कुंजी

4

है बहुत सरल:

FaqTrans::where(
      [ 
       'faq_id' => $faq_id, 
       'lang' => $lang 
      ] 
     )->update(
      [ 
       'body' => $body, 
       'title' => $title 
      ] 
     ); 
6

किसी भी व्यक्ति जो भविष्य में इस प्रश्न पर ठोकर खा रहा है (जैसे मैंने किया), यहां इलोक्वेन्ट में एक समग्र प्राथमिक कुंजी के लिए एक अच्छा कामकाज है।

यह अपने मॉडल वर्ग के शीर्ष पर चला जाता है:

public function save(array $options = array()) 
{ 
    if(!is_array($this->getKeyName())) 
     return parent::save($options); 

    // Fire Event for others to hook 
    if($this->fireModelEvent('saving') === false) 
     return false; 

    // Prepare query for inserting or updating 
    $query = $this->newQueryWithoutScopes(); 

    // Perform Update 
    if ($this->exists) { 
     if (count($this->getDirty()) > 0) { 
      // Fire Event for others to hook 
      if ($this->fireModelEvent('updating') === false) 
       return false; 

      // Touch the timestamps 
      if ($this->timestamps) 
       $this->updateTimestamps(); 

      // 
      // START FIX 
      // 

      // Convert primary key into an array if it's a single value 
      $primary = (count($this->getKeyName()) > 1) ? $this->getKeyName() : [$this->getKeyName()]; 

      // Fetch the primary key(s) values before any changes 
      $unique = array_intersect_key($this->original, array_flip($primary)); 

      // Fetch the primary key(s) values after any changes 
      $unique = !empty($unique) ? $unique : array_intersect_key($this->getAttributes(), array_flip($primary)); 

      // Fetch the element of the array if the array contains only a single element 
      $unique = (count($unique) <> 1) ? $unique : reset($unique); 

      // Apply SQL logic 
      $query->where($unique); 

      // 
      // END FIX 
      // 

      // Update the records 
      $query->update($this->getDirty()); 

      // Fire an event for hooking into 
      $this->fireModelEvent('updated', false); 
     } 

    // Perform Insert 
    } else { 
     // Fire an event for hooking into 
     if ($this->fireModelEvent('creating') === false) 
      return false; 

     // Touch the timestamps 
     if ($this->timestamps) 
      $this->updateTimestamps(); 

     // Retrieve the attributes 
     $attributes = $this->attributes; 

     if ($this->incrementing && !is_array($this->getKeyName())) 
      $this->insertAndSetId($query, $attributes); 
     else 
      $query->insert($attributes); 

     // Set exists to true in case someone tries to update it during an event 
     $this->exists = true; 

     // Fire an event for hooking into 
     $this->fireModelEvent('created', false); 
    } 

    // Fires an event 
    $this->fireModelEvent('saved', false); 

    // Sync 
    $this->original = $this->attributes; 

    // Touches all relations 
    if (array_get($options, 'touch', true)) 
     $this->touchOwners(); 

    return true; 
} 

मूल स्रोत:: https://github.com/laravel/framework/issues/5517#issuecomment-52996610

अद्यतन

protected $primaryKey = array('key1', 'key2'); 

तो फिर तुम इस के साथ अपने मॉडल पर save() विधि ओवरराइड 2016-05-03

मेरा नया पसंदीदा यह करने के लिए जिस तरह से: किसी को भी एक ही मुद्दा है

How I can put composite keys in models in Laravel 5?

-1

हैं। मैंने निशचित धनानी का जवाब दिया और यह बहुत अच्छी तरह से काम किया जो: इस लाइन को अपने मॉडल
सार्वजनिक $ प्राथमिकKey = '_id' में लिखें;
जहां आपकी मैनुअल प्राथमिक कुंजी

मेरे मामले के लिए है। मेरे पास एक उपयोगकर्ता तालिका थी और मेरे उपयोगकर्ताओं का हिस्सा छात्र हैं। इसलिए मेरी छात्रों की तालिका में प्राथमिक कुंजी "user_id" थी।
इस छात्र को मेरे छात्र मॉडल में जोड़ना: सार्वजनिक $ primaryKey = 'user_id';

मैं अपने छात्र ऑब्जेक्ट को अपडेट करते समय सेव() विधि का उपयोग करने में सक्षम था।

आशा इस

+1

बस दोहराने के लिए अन्य उत्तरों लिखने के बजाय, उत्तर देने वाले उत्तरों को उत्तर दें। –

संबंधित मुद्दे