Product、Prodpage、Field の 3 つのモデルがあります。
私は自分の PC のローカル mysql データベースに基づいてすべてのモデルをベイクするケーキ コンソールを作成しました。次に、パブリック $scaffold を使用して、モデルごとに単純なコントローラーを作成しました。ProductsController の例を次に示します。
<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
public $scaffold;
}
私のアプリ(localhost/cake/products)に入り、すべてうまくいきました。商品の追加、商品の削除、商品の編集ができました。次に、prodpages を追加し、フィールドを追加することもできます。私は先に進み、コントローラーとビューをベイクするために Cake コンソールを使用することにしました。$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
以下は、53 行目までの ProdpagesController です (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を初めて使用するので、チュートリアルを進めています。しかし、これは私を困惑させました。
更新: 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' => ''
)
);
}
これらのモデルをベイクするために Cake コンソールを作成したので、その中で関連付けを行いました。Prodpage と Product が正しく関連付けられていると思いました。1 つの製品に複数の Prodpage を含めることができます。1 つの Prodpage は 1 つの製品に属します。
クエリ エラーによる更新
したがって、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')
調べてみたところ、Prodpages テーブルの product_id 列に追加するドロップダウン リストの選択に関連付けられた product_id が渡されていません。