2016-12-03 61 views
0

我試圖創建一個表格,將數據添加到我的數據庫。我的表稱爲優惠券和我有以下代碼:錯誤:SQLSTATE [42000]:語法錯誤或訪問衝突:1066不是唯一的表/別名:'優惠券'

//CouponsController.php 
public function addCoupon() { 
    $coupon = $this->Coupons->newEntity(); 
     if ($this->request->is('post')) { 
     $coupon = $this->Coupons->patchEntity($coupon, $this->request->data); 
     if ($this->Coupons->save($coupon)) { 
      $this->Flash->success(__('The coupon has been saved.')); 
      return $this->redirect(['action' => 'index']); 
      } else { 
      $this->Flash->error(__('The coupon could not be saved. Please, try again.')); 
      } 
     } 
     $coupons = $this->Coupons->Coupons->find('list', ['limit' => 200]); 
     $this->set(compact('coupon', 'coupons')); 
     $this->set('_serialize', ['coupon']); 
} 

//CouponsTable.php 
public function initialize(array $config) 
{ 
    parent::initialize($config); 
    $this->table('coupons'); 
    $this->displayField('coupon_id'); 
    $this->primaryKey('coupon_id'); 
    $this->belongsTo('Coupons', [ 
     'foreignKey' => 'coupon_id', 
     'joinType' => 'INNER' 
    ]); 
} 

//add_coupon.ctp 
<h3>Add Coupon</h3> 
<?php 
    echo $this->Form->create(null,['url' => ['action' => 'addCoupon']]); 
    echo $this->Form->input('coupon_code'); 
    echo $this->Form->input('expiration_date'); 
    echo $this->Form->input('discount_amount'); 
    echo $this->Form->input('usage_limit'); 
    echo $this->Form->input('domain_limit'); 
    echo $this->Form->input('description'); 
    echo $this->Form->input('type'); 
    echo $this->Form->button('Submit'); 
    echo $this->Form->end(); 
?> 

當我點擊提交,數據被存儲在數據庫中,但我帶有「錯誤:SQLSTATE [42000]:語法錯誤或訪問違規:1066不是唯一表/別名:'優惠券'「。 SQL查詢: SELECT Coupons.coupon_id AS Coupons__coupon_id,Coupons.coupon_code AS Coupons__coupon_code,Coupons.expiration_date AS Coupons__expiration_date,Coupons.discount_amount AS Coupons__discount_amount,Coupons.usage_limit AS Coupons__usage_limit,Coupons.domain_limit AS Coupons__domain_limit,Coupons.description AS Coupons__description,優惠券。類型AS Coupons__type從優惠券優惠券INNER JOIN優惠券優惠券ON Coupons.coupon_id =(Coupons.coupon_id)LIMIT 20 OFFSET 0 那麼這裏有什麼問題?

回答

0

變化

$this->Coupons->Coupons->find('list', ['limit' => 200]) 

$this->Coupons->find('list', ['limit' => 200]) 

您導致表通過引用兩次參加對本身。

此外,刪除此代碼。我看不出有什麼理由加入桌子:

$this->belongsTo('Coupons', [ 
    'foreignKey' => 'coupon_id', 
    'joinType' => 'INNER' 
]); 
+0

我試過了,它沒有工作.. – user2817869

0

是您的主鍵是coupon_id嗎?如果是,那麼它違反了cakephp命名約定。

相關問題