我是CakePHP的新手,我有一个关系问题.
我的模型看起来像这样:
<?
class Operator extends AppModel
{
var $name = 'Operator';
var $hasOne = array('Contact','Adress','Land');
}
?>
Run Code Online (Sandbox Code Playgroud)
和我的表看起来像这样:
CREATE TABLE `operators` (
`id` varchar(45) NOT NULL,
`name` int(11) NOT NULL,
`adress_id` int(11) NOT NULL,
`land_id` int(11) NOT NULL,
`contact_id` int(11) NOT NULL,
`operator_category_id` int(11) NOT NULL,
`legal_form` varchar(45) DEFAULT NULL,
`affidavit` varchar(45) DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `operator_category_fk_idx` (`operator_category_id`),
KEY `contact_id_idx` (`contact_id`),
KEY `adress_id_idx` (`adress_id`),
KEY `land_id_idx` (`land_id`),
CONSTRAINT `adress_id` FOREIGN KEY (`adress_id`) REFERENCES `adresses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `contact_id` FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `land_id` FOREIGN KEY (`land_id`) REFERENCES `lands` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `operator_category_fk` FOREIGN KEY (`operator_category_id`) REFERENCES `operator_category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
hasOne如果您具有1 to 1能够访问模型中没有外键的关联模型的关联,则仅用于关系.
在你的情况,你要使用belongsTo,belongsTo是只要你有你的模型指向另一个模型的外键使用,你的Operator模型有外键指向Contact,Address,Land这是RESP.contact_id,address_id和land_id.
由于该contact_id领域是在Operator模型中,然后将Operator模型属于关联的Contact模型.
class Operator extends AppModel {
public $belongsTo = array('Contact') ;
}
Run Code Online (Sandbox Code Playgroud)
在获取Operator条目时,您将获得以下内容:
Array(
[Operator] => Array
(
[id] => 42,
[name] => 'operator',
[contact_id] => 33
)
[Contact] => Array
(
[id] => 33,
[name] => 'contact'
)
)
Run Code Online (Sandbox Code Playgroud)
由于你的Operator belongsTo Contact,你的Contact hasOne OR hasMany Operator.让我们来说明两者之间的区别:
Contact条目相关的只有一个 Operator,你可以添加一个operator_id外键的内部Contact模型,但它是多余的,因为你已经有使用联想contact_id在Operator.因此,您所做的是在模型中创建一个hasOne关联Contact,因此Contact hasOne Operator.class Contact extends AppModel {
public $hasOne = array('Operator') ;
}
Run Code Online (Sandbox Code Playgroud)
同样,在获取Contact条目时,您将获得:
Array(
[Contact] => Array
(
[id] => 33,
[name] => 'contact'
)
[Operator] => Array
(
[id] => 42,
[name] => 'operator',
[contact_id] => 33
)
)
Run Code Online (Sandbox Code Playgroud)
请注意,这Operator与使用belongsTo进行fecting 时相同Contact.hasOne和之间的区别belongsTo主要是哪个模型有一个指向另一个的外键.
Contact输入的Eah 与多个(很多)相关联Operator,在这种情况下您的Contact hasMany Operator.class Contact extends AppModel {
public $hasMany = array('Operator') ;
}
Run Code Online (Sandbox Code Playgroud)
再次,输出$this->Contact->find():
Array(
[Contact] => Array
(
[id] => 33,
[name] => 'contact'
)
[Operator] => Array
(
[0] => Array
(
[id] => 42,
[name] => 'operator 42',
[contact_id] => 33
)
[0] => Array
(
[id] => 47,
[name] => 'operator 47',
[contact_id] => 33
)
)
)
Run Code Online (Sandbox Code Playgroud)
现在,假设一个Operator可以有多个Contact,一个Contact可以服务多个Operator.在这种情况下,您需要一个额外的表(operators_contacts如果您遵循CakePHP命名约定,应该调用它),有两个字段operator_id和contact_id:
class Contact extends AppModel {
public $hasAndBelongsToMany = array('Operator') ;
}
Run Code Online (Sandbox Code Playgroud)
class Operator extends AppModel {
public $hasAndBelongsToMany = array('Contact') ;
}
Run Code Online (Sandbox Code Playgroud)
输出$this->Contact->find('all')将类似于使用hasMany关系的输出,主要区别在于它们是否为模型中的字段operator_id或contact_id字段.
假设我有以下型号:Company,Employee,Address,和以下限制:
Company有各种Employee但只有一种CEO是其中的一部分EmployeeCompany有一个Address,每个最多只有一家公司Address您有以下表格:
CREATE TABLE companies (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
ceo_id INTEGER REFERENCES employees (id)
) ;
CREATE TABLE employees (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
company_id INTEGER REFERENCES companies (id)
) ;
CREATE TABLE addresses (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
company_id INTEGER REFERENCES companies (id)
) ;
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,你不能所有的约束条件表,因为你有之间的循环companies和employees,你需要后添加的约束.
以下CakePHP模型:
class Company extends AppModel {
/* A Company has many Employee. */
public $hasMany = array('Employee') ;
/* A Company has one CEO, but the key is in the Company model,
so it is a belongsTo relationship. */
public $belongsTo = array(
'CEO' => array(
'className' => 'Employee', // It's an Employee
'foreignKey' => 'ceo_id'
)
) ;
/* A Company has one address. */
public $hasOne = array('Address') ;
} ;
Run Code Online (Sandbox Code Playgroud)
class Employee extends AppModel {
/* An Employee belongs to a Company. */
public $belongsTo = array('Company') ;
} ;
Run Code Online (Sandbox Code Playgroud)
class Address extends AppModel {
/* An address belongs to a Company. */
public $belongsTo = array('Company') ;
} ;
Run Code Online (Sandbox Code Playgroud)
请注意,我已经把一个company_id外键的Address模式,所以Address belongsTo Company和Company hasOne Address,我可以把address_id里面Company的模型,我将不得不Company belongsTo Address和Address hasOne Company.我在这里做了一个随意的选择,在一个真实的应用程序中你应该想到上面两个案例中哪一个是最有意义的.
另外请注意,我定义我的模型的方式,没有什么防止Employee成为CEO一个Company不为自身的一个Employee.
您可以在CakePHP 2.x Book中找到更多信息.