可能重复:
CakePHP: Call to a member function find() on a non-object
我有三个型号:Product,Prodpage,Field。
我做了蛋糕控制台,根据我的电脑上的本地mysql数据库烘焙所有模型。然后我使用public $ scaffold为每个模型创建了一个简单的控制器。这是ProductsController示例:
<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
public $scaffold;
}
进入我的应用程序(localhost / cake / products),一切正常。我可以添加产品,删除产品,编辑产品。然后我可以添加prodpages,我也可以添加字段。我决定继续使用蛋糕控制台来烘焙控制器和视图。我的理解是它应该与$ scaffold做同样的事情,但这次控制器应该有更多的代码。因此允许我开始更多地定制它。
我回到localhost / cake / products,它运行正常。然后,当我尝试转到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的新手,所以我正在学习教程。这让我很难过。
更新:Model / 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' => ''
)
);
}
这是Model / 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和产品相关。一种产品可以有很多产品。一个产品属于一个产品。
更新W / QUERY ERROR
因此,当我转到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列中..任何想法为什么?
答案 0 :(得分:1)
看起来你的模型名称是复数,它们应该是单数。例如:
public $belongsTo = array(
// Product, not Products
'Product' => array(
'className' => 'Product',
'foreignKey' => 'products_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);