对非对象cakephp调用成员函数find()

对非对象cakephp调用成员函数find(),php,cakephp,Php,Cakephp,可能重复: 我有三种型号:产品、产品页、字段 我在我的pc上基于我的本地mysql数据库制作了cake控制台来烘焙所有模型。然后,我使用public$scaffold为每个模型创建了一个简单的控制器。以下是ProductsController示例: <?php // app/Controller/ProductsController.php class ProductsController extends AppController { public $scaffold; } pu

可能重复:

我有三种型号:产品、产品页、字段

我在我的pc上基于我的本地mysql数据库制作了cake控制台来烘焙所有模型。然后,我使用public$scaffold为每个模型创建了一个简单的控制器。以下是ProductsController示例:

<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
  public $scaffold;
}
public $belongsTo = array(
    // Product, not Products
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'products_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
以下是ProdpagesController,它们一直贯穿第53行(添加函数):

有人知道我做错了什么,或者详细说明错误告诉了我什么?我是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' => ''
        )
    );

}

我查看了它,它没有将与下拉列表选择关联的product_id传递到我的Prodpages表的product_id列中。。你知道为什么吗?

看起来你的模特名字是复数,而它们应该是单数。例如:

<?php
// app/Controller/ProductsController.php
class ProductsController extends AppController {
  public $scaffold;
}
public $belongsTo = array(
    // Product, not Products
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'products_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

这意味着
$this->Prodpage->Product
不存在,这可能是因为Prodpage与Product无关。我用Product和Prodpage模型编辑了这篇文章。我想我把它们联系对了。。还有别的事吗?还是我做错了?你的型号有问题。你的产品应该是单数的:产品。如果您的型号名称是真正的产品,请使用
$this->Prodpage->Products
。田里也一样,谢谢!我看着它,确信它与此有关。我还有最后一个问题。当我尝试添加新的Prodpage时,它会生成一个下拉框,允许我选择与之关联的产品。当我选择产品时。。它根本没有在sql查询中传递产品下拉列表值,从而导致错误。我在主帖中添加了信息。我添加了答案,这样你就可以接受了。至于第二个问题,我认为这是一个单独的问题。您还需要发布HTML表单。我猜这是一个问题,你的foreignKey定义再次是复数(在模型关系中),但在表中是单数。
<?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' => ''
        )
    );

}
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')
public $belongsTo = array(
    // Product, not Products
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'products_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);