0

可能重复:
CakePHP:调用非对象上的成员函数 find()

我有三个模型:Product、Prodpage、Field。

我做了蛋糕控制台来烘烤基于我电脑上本地 mysql 数据库的所有模型。然后,我使用公共 $scaffold 为每个模型创建了一个简单的控制器。这是 ProductsController 示例:

<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
  public $scaffold;
}

进入我的应用程序(本地主机/蛋糕/产品),一切正常。我可以添加产品、删除产品、编辑产品。然后我可以添加 prodpages,也可以添加字段。我决定继续使用蛋糕控制台来烘焙控制器和视图。我的理解是它应该和 $scaffold 做同样的事情,但是这次控制器应该有更多的代码。因此,我可以开始对其进行更多定制。

我回到本地主机/蛋糕/产品,它工作正常。然后,当我尝试转到 localhost/cake/prodpages/add 时,我收到此错误:

Fatal error: Call to a member function find() on a non-object in C:\wamp\www\cake\app\Controller\ProdpagesController.php on line 50

这是 ProdpagesController 一直到第 53 行(添加函数):

<?php
App::uses('AppController', 'Controller');
/**
 * Prodpages Controller
 *
 * @property Prodpage $Prodpage
 */
class ProdpagesController extends AppController {


/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Prodpage->recursive = 0;
        $this->set('prodpages', $this->paginate());
    }

/**
 * view method
 *
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        $this->Prodpage->id = $id;
        if (!$this->Prodpage->exists()) {
            throw new NotFoundException(__('Invalid prodpage'));
        }
        $this->set('prodpage', $this->Prodpage->read(null, $id));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Prodpage->create();
            if ($this->Prodpage->save($this->request->data)) {
                $this->Session->setFlash(__('The prodpage has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The prodpage could not be saved. Please, try again.'));
            }
        }
        $products = $this->Prodpage->Product->find('list');
        $this->set(compact('products'));
    }

这是第50行,

$products = $this->Prodpage->Product->find('list');

任何人都知道我在这里做错了什么或详细说明错误告诉我什么?我是 cakephp 的新手,所以我正在浏览教程。这让我很难过。

更新:型号/Product.php

<?php
App::uses('AppModel', 'Model');
/**
 * Product Model
 *
 * @property Prodpages $Prodpages
 */
class Product extends AppModel {
/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'product_name';
/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'product_name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        's7_location' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'created' => array(
            'datetime' => array(
                'rule' => array('datetime'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'modified' => array(
            'datetime' => array(
                'rule' => array('datetime'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */

    public $hasMany = array(
        'Prodpages' => array(
            'className' => 'Prodpages',
            'foreignKey' => 'id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

这是模型/Prodpage.php

<?php
App::uses('AppModel', 'Model');
/**
 * Prodpage Model
 *
 * @property Products $Products
 * @property Fields $Fields
 */
class Prodpage extends AppModel {
/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'page_name';


/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'is_blank' => array(
            'boolean' => array(
                'rule' => array('boolean'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'page_order' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        's7_page' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'created' => array(
            'datetime' => array(
                'rule' => array('datetime'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'modified' => array(
            'datetime' => array(
                'rule' => array('datetime'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Products' => array(
            'className' => 'Products',
            'foreignKey' => 'products_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Fields' => array(
            'className' => 'Fields',
            'foreignKey' => 'id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

我做了蛋糕控制台来烘焙这些模型,所以我在那里做了关联。我以为我的 Prodpage 和 Product 相关的正确。一个产品可以有多个 Prodpages。一个 Prodpage 属于一种产品。

更新有查询错误

因此,当我转到 localhost/cake/prodpages/add 并填写信息,从产品下拉列表中选择产品时,我收到此错误

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`builder_cake`.`prodpages`, CONSTRAINT `fk_prodpages_products` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

SQL Query: INSERT INTO `builder_cake`.`prodpages` (`page_name`, `is_blank`, `page_order`, `s7_page`, `modified`, `created`) VALUES ('Page 2', '0', 2, 2, '2012-06-13 16:51:35', '2012-06-13 16:51:35')

我调查了一下,它没有传递与下拉列表选择关联的 product_id 以添加到我的 Prodpages 表中的 product_id 列中。有什么想法吗?

4

1 回答 1

1

看起来您的模型名称是复数,而它们应该是单数。例如:

public $belongsTo = array(
    // Product, not Products
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'products_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
于 2012-06-13T19:10:14.917 回答