2012-07-23 62 views
-1

我在這裏試圖理解的是,這個錯誤是什麼意思,我做錯了什麼?我必須搞砸命名約定。CakePHP:過濾器和外鍵!不工作

我的模特兒是產品和類別。類別具有hasMany並且產品具有belongsTo。

http://webdesign4.georgianc.on.ca/~100141468/comp2084/todo/products/add

http://webdesign4.georgianc.on.ca/~100141468/comp2084/todo/products/filter/9

姓名Y

套印Y

的ProductsController

function filter($category_id) { 
      $this->set('Product',$this->Product->findAllByCategoryId($category_id)); 
     } 

添加

$this->loadModel('Category'); 
$this->set('Categorys',$this->Category->find('list',array('order'=> array('Category.name')))); 

filter.ctp

<? foreach($Product as $row): ?> 
    <tr><td> 
<?=$row['Product']['id']?> 
</td><td> 
<?=$row['Product']['name']?> 
</td><td> 
<?=$row['Product']['price']?> 
</td><td> 
<?=$row['Category']['name']?> 
</td><td> 
<a href="edit/<?=$row['Product']['id']?>">Edit</a> 
    </td></tr> 
<? endforeach; ?> 

add.ctp

<?php 
     echo $this ->Form->input('name'); 
     echo $this ->Form->input('description'); 
     echo $this ->Form->input('price'); 
     echo $this ->Form->input('file', array('type' => 'file')); 
     echo $this ->Form->input('Category_id'); 
     echo $this ->Form->end('submit',true); 
     ?> 

回答

2

開始做一個debug($Product);你看到一個Category關鍵?如果沒有,則設置遞歸更高或者更好使用Containable

你的附加組件,視圖VAR名稱更改爲categories和表單輸入字段更改爲category_id(也就是說,如果你已經按照約定在數據庫中,這只是一個錯字)

如果類是有關產品,不需要loadModel

根本就$this->Product->Category->find...

+0

我該在哪裏進行調試? – 2012-07-23 20:34:32

+0

只是在視圖 – tigrang 2012-07-23 20:35:02

+0

呃,它可以安全地說,它沒有正確連接,因爲過濾器不工作? – 2012-07-23 20:38:56

1

,這是非常simple..you不抓餅基本面。我建議再次閱讀本書和教程,重點關注命名約定和關聯。

您到位需要這些協會:

//product.php 
var $belongsTo = array(
    'Category' => array(
     'className' => 'Category', 
     'foreignKey' => 'category_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    )); 


// category.php 
var $hasMany = array(
    'Product' => array(
     'className' => 'Product', 
     'foreignKey' => 'category_id' 
    ) 
); 



// products_controller.php 
function filter($category_id) { 
    $this->set('products', $this->Product->findAllByCategoryId($category_id)); 
} 

function add() { 
    if (!empty($this->data)) { 
     $this->Product->create(); 
     if ($this->Product->save($this->data)) { 
      $this->Session->setFlash(__('The Product has been saved', true)); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The Product could not be saved. Please, try again.', true)); 
     } 
    } 
    $categories = $this->Product->Category->find('list'); 
    $this->set(compact(array('categories'))); 
} 

    // filter.ctp 
    debug($products); // just to see what data has been returned 

    // add.ctp 
    echo $this->Form->create('Product'); 
    echo $this->Form->input('name'); 
    echo $this->Form->input('description'); 
    echo $this->Form->input('price'); 
    echo $this->Form->input('file', array('type' => 'file')); 
    echo $this->Form->input('category_id'); // categories 
    echo $this->Form->end('submit',true); 

清除緩存,並且這應該工作。

+0

我得到這個致命錯誤:調用成員函數find()在非對象在/home/100141468/public_html/comp2084/todo/app/Controller/ProductsController.php在24行對於productsController – 2012-07-23 22:02:04

+0

這是什麼行?根據我的代碼,它只能是'$ this-> Product-> Category-> find()';這表明您的模型仍未設置或緩存尚未清除。 – Ross 2012-07-24 09:17:35