我有一个名为“客户”的表,具有以下(缩短的)模式:
id | customer_id | type | salutation | forename | surname | created | modified
在这个表中,我想存储的人,这可能是相关的:
id | customer_id | type | salutation | forename | surname | created | modified 1 | NULL | husband | Mr. | John | Doe | 2016-01-05 10:00:00 | 2016-01-05 10:00:00 2 | 1 | wife | Mrs. | Jane | Doe | 2016-01-05 10:01:00 | 2016-01-05 10:01:00 3 | 1 | child | Mr. | Jim | Doe | 2016-01-05 10:02:00 | 2016-01-05 10:02:00
“customer_id”=NULL的客户是主客户,但#2和#3表示#1。
我已经在phpmyadmin中创建了表,并且没有错误地执行了“bin/cake bake all customers”。然后我创建了“主客户”。创建第二个帐户时,我希望选择字段显示customer#1,但下拉字段为空。
模型:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('customers');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->hasMany('Customers', [
'foreignKey' => 'customer_id'
]);
}
如果您需要进一步的信息或更多的代码,请告诉我<非常感谢。
向马丁问好
SQL繁殖
CREATE TABLE IF NOT EXISTS `customers` (
`id` int(11) NOT NULL,
`typ` varchar(10) NOT NULL,
`customer_id` int(11) DEFAULT NULL,
`salutation` varchar(4) NOT NULL,
`prename` varchar(255) NOT NULL,
`surname` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
INSERT INTO `customers` (`id`, `typ`, `customer_id`, `salutation`, `prename`, `surname`, `created`, `modified`) VALUES
(1, 'husband', NULL, 'Mr.', 'John', 'Doe', '2016-03-02 21:26:32', '2016-03-02 21:26:32'),
(2, 'wife', 1, 'Ms.', 'Jane', 'Doe', '2016-03-02 21:27:25', '2016-03-02 22:10:05'),
(3, 'child', 1, 'Mr.', 'Jim', 'Doe', '2016-03-02 21:27:41', '2016-03-02 22:10:15');
ALTER TABLE `customers` ADD PRIMARY KEY (`id`), ADD KEY `customers_fk0` (`customer_id`);
ALTER TABLE `customers` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
ALTER TABLE `customers` ADD CONSTRAINT `customers_fk0` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`);
比,跑
bin/cake bake all customers
编辑我已检查,可能的副本,并更改了我的模型
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->hasMany('ChildCustomers', [ // <-- changed this line
'className' => 'Customers', // <-- added this line
'foreignKey' => 'customer_id'
]);
现在,如果我尝试添加/编辑,对选择没有任何更改。但如果我查看现有客户,我得到:
错误:SQLSTATE[42000]:语法错误或访问冲突:1066非唯一表/别名:“客户”
这是用于显示选择的代码:
echo $this->Form->input('customer_id');
此代码由“bin/cake bake all customers”生成
编辑2
我将总结我目前的状态:
型号:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('customers');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('ParentCustomers', [
'className' => 'Customers',
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->hasMany('ChildCustomers', [
'className' => 'Customers',
'foreignKey' => 'customer_id'
]);
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['customer_id'], 'ParentCustomers'));
return $rules;
}
控制器:
public function view($id = null)
{
$customer = $this->Customers->get($id, [
'contain' => ['ParentCustomers']
]);
$this->set('customer', $customer);
$this->set('_serialize', ['customer']);
}
如果用户有关联(妻子、孩子),则视图在页面底部显示标题“相关客户”,但没有显示相关客户的附加表。如果用户具有“customer_id=0”,视图将显示“在表“customers”中未找到记录”。
我还加了一句
$this->set('customers', $this->Customers->find('list'));
添加()和编辑()函数,但我发现没有办法也允许一个空值。
澄清:稍后,首页(index())应该只列出customer_id=0的“主客户”,如果他有妻子和/或孩子,还应该列出一个小嵌套表。
我想我走对了。。。是吗?
再次表示感谢
对于第一个关联,您还应该使用ParentCustomers
<代码>客户复制自身。
$this->belongsTo('ParentCustomers', [ // <-- You need to change here
'className' => 'Customers', // <-- You need to change here
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->hasMany('ChildCustomers', [
'className' => 'Customers',
'foreignKey' => 'customer_id'
]);
您确定已在控制器中加载模型吗?
将以下内容放入控制器的初始化():$this-
编辑:这就像:
public function initialize()
{
parent::initialize();
$this->loadModel('Customers');
}
我使用CakePHPs TreeBehavior解决了这个问题。
我使用parent_id而不是customer_id,并添加了右和左列,如博客教程-第3部分所述。
它现在正在为我工作!谢谢大家!