2012-07-23 101 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