我正在用cakePHP構建一個應用程序,而且我很新。我現在想做的是這個。讓我用幾句話來解釋:我有2個模型,Item和Typologies。一個項目可以有許多類型。所以類型學表有一個外鍵 - item_id - 它指的是項目。現在我想阻止用戶刪除項目,如果仍然有類型引用這個項目。如果其他對象引用它,則拒絕刪除對象
我的產品型號是:
<?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;
}
}
而當我試圖刪除一個項目有typologes或一個項目它不顯示這個e RROR!
Fatal Error
Error: Call to a member function find() on a non-object
File: C:\wamp\www\project\app\Model\Item.php
Line: 449
我該如何解決它!
Thanx a Lot @savedario,你的代碼真的幫了我。我得到了Exacly我想要的東西。 – landi