【发布时间】:2014-02-26 15:43:06
【问题描述】:
我正在使用 cakePHP 构建一个应用程序,而且我对它还很陌生。我现在想做的就是这个。让我用几句话解释一下:我有 2 个模型,项目和类型。一个项目可以有多种类型。所以 Typology 表有一个外键 - item_id - 它指的是 item。现在我想阻止用户删除项目,如果仍然有引用这个项目的类型。
我的物品型号是这样的:
<?php
App::uses('AppModel', 'Model');
/**
* Item Model
*
* @property ItemLocation $ItemLocation
* @property ItemCharacteristic $ItemCharacteristic
* @property FirstSeller $FirstSeller
* @property SecondSeller $SecondSeller
* @property User $User
* @property Contact $Contact
* @property ItemPicture $ItemPicture
* @property Typology $Typology
*/
class Item extends AppModel {
public $name = 'Item';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'id';
/**
* Display field
*
* @var string
*/
public $displayField = 'title';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'id' => array(
'blank' => array(
'rule' => 'blank',
'on' => 'create',
),
),
'title' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Item name can only contain letters, numbers and spaces.',
),
'maxLength' => array(
'rule' => array('maxLength', 100),
'message' => 'The Item name must not be longer than 100 characters.',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'The Item name must not be empty.',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This Item name already exists.',
),
),
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Not Empty',
),
),
'created' => array(
'datetime' => array(
'rule' => array('datetime'),
//'message' => 'Your custom message here',
),
),
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'ItemUser' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'ItemTypologies' => array(
'className' => 'Typology',
'foreignKey' => 'item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
类型学模型是这样的:
<?php
App::uses('AppModel', 'Model');
/**
* Typology Model
*
* @property Item $Item
* @property TypologyCategory $TypologyCategory
* @property TypologyCondition $TypologyCondition
* @property User $User
* @property TypologyPicture $TypologyPicture
*/
class Typology extends AppModel {
public $name = 'Typology';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'id';
/**
* Display field
*
* @var string
*/
public $displayField = 'title';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'id' => array(
'blank' => array(
'rule' => 'blank',
'on' => 'create',
),
),
'item_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Chose Which Object This Typology Belongs To',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Can Not be Empty',
),
),
'title' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Typology name can only contain letters, numbers and spaces.',
),
'maxLength' => array(
'rule' => array('maxlength', 50),
'message' => 'The Typology name must not be longer than 50 characters.',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Typology Title Can not be Empty',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'Typology Name Should be Unique',
),
),
'description' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Typology name can only contain letters, numbers and spaces.',
),
'maxLength' => array(
'rule' => array('maxlength', 350),
'message' => 'The Typology name must not be longer than 350 characters.',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Description can not be Empty',
),
),
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Chose the user who created this typology',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
),
),
'created' => array(
'datetime' => array(
'rule' => array('datetime'),
//'message' => 'Your custom message here',
),
),
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'TypologyItem' => array(
'className' => 'Item',
'foreignKey' => 'item_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'TypologyUser' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
现在我看到和写的是这个,如果有人有任何想法,我真的很感激:
// using app/Model/Item.php
// In the following example, do not let an Item to be deleted if it
// still contains Typologies Attached to it.
// A call of $this->Typology->delete($id) from TypologiesController.php has set
// $this->id .
// Assuming 'ItemTypologies hasMany Typology', we can access $this->Typoogy
// in the model.
public function beforeDelete($cascade = true) {
$count = $this->Typology->find('count', array('conditions' => array('item_id' => $this->Typology->id)));
if ($count == 0) {
return true;
} else {
return false;
}
}
当我尝试删除具有类型错误的项目或不显示此错误的项目时!
Fatal Error
Error: Call to a member function find() on a non-object
File: C:\wamp\www\project\app\Model\Item.php
Line: 449
我该如何解决!!
【问题讨论】: