我有一个名为“customers”的表,其架构如下(缩短):
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
Customers with "customer_id" = NULL are the master customers, but #2 and #3 refers to #1.
I've created the table in phpmyadmin and do: "bin/cake bake all customers" without errors. Then I created the 'master customer'. When I create the second account, I expect that the select-field shows customer #1, but the dropdown-field is empty.
The model:
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
<小时/>
编辑 我查了一下,possible duplicate ,并改变了我的模型
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->hasMany('ChildCustomers', [ // <-- changed this line
'className' => 'Customers', // <-- added this line
'foreignKey' => 'customer_id'
]);
现在,如果我尝试添加/编辑,则不会更改选择。但如果我查看现有客户,我会得到:
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Customers'
这是显示选择的代码:
echo $this->Form->input('customer_id');
此代码是由“bin/cake eat allcustomers”生成的
<小时/>编辑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;
}
Controller :
public function view($id = null)
{
$customer = $this->Customers->get($id, [
'contain' => ['ParentCustomers']
]);
$this->set('customer', $customer);
$this->set('_serialize', ['customer']);
}
如果用户有关联(妻子、 child ),则 View 会在页面底部显示标题“相关客户”,但不会显示其他显示相关客户的表格。如果用户具有“customer_id = 0”,则 View 显示“在表“customers”中找不到记录”。
我还添加了
$this->set('customers', $this->Customers->find('list'));
到 add() 和 edit() 函数,但我发现没有办法允许空值。
澄清一下: 稍后,首页 (index()) 应该只列出 customer_id = 0 的“主客户”和一个小嵌套表,如果他有妻子和/或 child 的话。
我认为我走在正确的道路上......是吗?
再次感谢
最佳答案
您还应该使用 ParentCustomers
作为第一个关联。 Customers
重复自身。
$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'
]);
关于php - CakePHP3 在同一模型中将表链接在一起(内连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597577/