Possible Duplicate:
CakePHP: Call to a member function find() on a non-object調用一個成員函數find()方法在非對象CakePHP的
我有三個型號:產品,Prodpage,現場。
我做了蛋糕控制檯來烘烤基於我的本地mysql數據庫在我的電腦上的所有模型。然後,我使用公共$腳手架爲每個模型創建了一個簡單的控制器。下面是ProductsController的例子:
<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
public $scaffold;
}
走進我的應用程序(本地主機/蛋糕/產品)和一切工作正常。我可以添加產品,刪除產品,編輯產品。然後我可以添加產品,我也可以添加字段。我決定繼續使用蛋糕控制檯來烘烤控制器和視圖。我的理解是它應該和$ scaffold做同樣的事情,但是這次控制器應該有更多的代碼。因此,讓我開始定製它多一點。
我回到本地主機/蛋糕/產品,它工作正常。然後,當我嘗試到本地主機/蛋糕/ prodpages /添加和我得到這個錯誤:
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(add函數):
<?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屬於一個產品。
UPDATE W /查詢錯誤
所以,當我去到localhost /蛋糕/ prodpages /添加和填寫信息,從產品下拉列表中我得到這個錯誤選擇產品
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加入到到的product_id列在我的Prodpages表..任何想法,爲什麼?
這意味着'$ this-> Prodpage-> Product'不存在,這可能是因爲Prodpage與產品無關。 – jeremyharris
我使用Product和Prodpage模型編輯了帖子。我以爲我有他們正確的相關..還有別的嗎?還是我做錯了他們? – thindery
您的型號名稱存在問題。你有產品的** **它應該是單數:產品。如果您的型號名稱是真正的產品,則使用'$ this-> Prodpage-> Products'。菲爾茲同樣如此。 – jeremyharris