Cakephp在模型中声明关系时
当我在模型中声明关系时 示例 ::
Class User extends AppModel{
var $name = 'User';
**var $hasOne = 'Myprofile';**
Class User extends AppModel{
var $name = 'User';
**var $hasMany = 'Reply';**
中声明,
Class Myprofile extends AppModel{
var $name ='Myprofile';
var $primaryKey = 'myprofileid';
**var $hasOne = 'User';**
Class Reply extends AppModel{
var $name ='Myprofile';
var $primaryKey = 'myprofileid';
**var $belongsTo = 'User'**
和我必须在正确与否
而另一个问题
<代码> 用户{
用户ID
姓名
}
我的小部件{
我的小部件ID
小部件名称
}
小部件使用{
widgetuseid
我的widget_id
widget_id
Class Mywidget extends AppModel{
var $name = 'Mywidget';
var $primaryKey = 'mywidgetid';
var $hasAndBelongsToMany = array(
'Mywidget' => array(
'className' => 'Mywidget',
'joinTable' => 'Widgetuse',
'foreignKey' => 'user_id',
'associationForeignKey' => 'mywidget_id',
'with' => 'Widgetuse',
right or not
thank you for comment
when I declare relationship in Model
Example ::
Class User extends AppModel{
var $name = 'User';
**var $hasOne = 'Myprofile';**
Class User extends AppModel{
var $name = 'User';
**var $hasMany = 'Reply';**
and I must be declare in
Class Myprofile extends AppModel{
var $name ='Myprofile';
var $primaryKey = 'myprofileid';
**var $hasOne = 'User';**
Class Reply extends AppModel{
var $name ='Myprofile';
var $primaryKey = 'myprofileid';
**var $belongsTo = 'User'**
Right or Not
And the other a questionuser{
userid
name
}
mywidget{
mywidgetid
widgetname
}
widgetuse{
widgetuseid
mywidget_id
widget_id
Class Mywidget extends AppModel{ var $name = 'Mywidget'; var $primaryKey = 'mywidgetid'; var $hasAndBelongsToMany = array( 'Mywidget' => array( 'className' => 'Mywidget', 'joinTable' => 'Widgetuse', 'foreignKey' => 'user_id', 'associationForeignKey' => 'mywidget_id', 'with' => 'Widgetuse', right or not thank you for comment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,停止使用“userid”和“profileid”作为主键,改用“id”(默认情况下)。
个人资料属于用户,用户有很多回复;回复属于用户。
另一个问题是什么?
First, stop using "userid" and "profileid" as Primary keys, use "id" instead (as it's by default).
Profile belongsTo User, User hasMany Reply; Reply belongsTo User.
What is the other question?