Possible Duplicate:
CakePHP: Call to a member function find() on a non-object
I have three Models: Product, Prodpage, Field.
I did the cake console to bake all models based on my local mysql db on my pc. I then created a simple controller for each model utilizing the public $scaffold. Here is the ProductsController example:
<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
public $scaffold;
}
went into my app (localhost/cake/products) and everything worked fine. I could add products, delete products, edit products. I could then add prodpages, and I could also add the fields. I decided to go ahead and use the cake console to bake the controllers and views. It was my understanding that it should do the same thing as the $scaffold, but this time the controllers should have more of the code in it. Thus allowing me to start to customize it a bit more.
I go back to localhost/cake/products and it was working fine. Then when I try to go to localhost/cake/prodpages/add and I get this error:
Fatal error: Call to a member function find() on a non-object in C:\wamp\www\cake\app\Controller\ProdpagesController.php on line 50
Here is the ProdpagesController all they way through line 53(the add function):
<?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'));
}
and this is line 50,
$products = $this->Prodpage->Product->find('list');
Anybody know what I am doing wrong here or elaborate on what the error is telling me? I’m new to cakephp so I am walking through tutorials. This has me stumped though.
Update: 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' => ''
)
);
}
Here is 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' => ''
)
);
}
I did the cake console to bake these models, so I did the associations within there. I thought I had Prodpage and Product related correctly. One product can have many Prodpages. One Prodpage belongs to one product.
UPDATE W/ QUERY ERROR
So when I go to localhost/cake/prodpages/add and fill out the info, selecting a Product from the product dropdown list I get this error
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')
I looked into to it and it is not passing the product_id associated with the dropdown list selection to add into into the product_id column in my Prodpages table.. any thoughts why?
It looks like your model names are plural, when they should be singular. For example: