저는 cakePHP로 응용 프로그램을 작성하고 있습니다. 내가 지금하고 싶은 것은 이것이다. 제가 몇 마디로 설명 드리겠습니다 : 저는 2 가지 모델, 항목과 유형학을 가지고 있습니다. 하나의 항목은 많은 유형을 가질 수 있습니다. 그래서 Typology 테이블은 foreign key를 가지고 있습니다 - 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' => ''
)
);
, 그리고 사람이 어떤 생각을 가지고 있다면 정말 좋겠 고마워요 :
그리고 typologes가있는 항목을 삭제하려고 할 때 또는이 항목을 표시하지 않는 항목을 삭제하려고 할 때 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