CakePHP - 关系问题 - Posts.Child属于用户,如何定义?
下面是一个帖子模型,正如您所看到的,我在每个帖子上都有一个“belongsTo”关系,因此用户详细信息也会随帖子一起检索。 我还希望 Posts.Children 具有完全相同的行为(请参阅下面的 hasMany 定义)。
问题:
如何将“belongsTo”关系应用到“Posts.Children”(“Posts.Child ownsTo User”)?
<?php
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'post_owner',
'fields' => array( "User.id" , "User.first_name" , "User.last_name" )
)
);
// -=>
var $hasMany = array(
'Child' => array(
'className' => 'Post',
'foreignKey' => 'parent_id',
'conditions' => array( "Child.active" => "1" ),
'limit' => '2',
'order' => 'time DESC'
)
);
}
?>
这是我当前正在使用的 PostsController 代码:
class PostsController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Posts';
function index() {
$conditions = array( "Post.parent_id" => "0" , "Post.active" => "1" );
$this->set( 'posts', $this->Post->find('all',array( 'conditions'=>$conditions )) );
debug( $this->Post->find('all',array( 'conditions'=>$conditions )) );
}
“如果在帖子控制器中调用 debug($this->Post->Child); ,输出是什么?” 这里是:
Post Object
(
[name] => Post
[hasMany] => Array
(
[Child] => Array
(
[className] => Post
[foreignKey] => parent_id
[conditions] => Array
(
[Child.active] => 1
)
[fields] =>
[order] =>
[limit] =>
[offset] =>
[dependent] =>
[exclusive] =>
[finderQuery] =>
[counterQuery] =>
)
)
[belongsTo] => Array
(
[User] => Array
(
[className] => User
[foreignKey] => dom
[fields] => Array
(
[0] => User.id
[1] => User.first_name
[2] => User.last_name
)
[conditions] =>
[order] =>
[counterCache] =>
)
)
[useDbConfig] => default
[useTable] => posts
[displayField] => title
[id] =>
[data] => Array
(
)
[table] => posts
[primaryKey] => id
[_schema] => Array
(
[active] => Array
(
[type] => integer
[null] =>
[default] => 1
[length] => 1
)
[id] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
[key] => primary
)
[parent_id] => Array
(
[type] => integer
[null] =>
[default] => 0
[length] => 11
)
[time] => Array
(
[type] => integer
[null] =>
[default] => 0
[length] => 11
)
[dom] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
)
[sub] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
)
[created] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[modified] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[text] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 4096
[collate] => latin1_swedish_ci
[charset] => latin1
)
)
[validate] => Array
(
)
[validationErrors] => Array
(
)
[tablePrefix] =>
[alias] => Child
[tableToModel] => Array
(
[posts] => Child
[users] => User
)
[logTransactions] =>
[cacheQueries] =>
[hasOne] => Array
(
)
[hasAndBelongsToMany] => Array
(
)
[actsAs] =>
[Behaviors] => BehaviorCollection Object
(
[modelName] => Child
[_attached] => Array
(
)
[_disabled] => Array
(
)
[__methods] => Array
(
)
[__mappedMethods] => Array
(
)
)
[whitelist] => Array
(
)
[cacheSources] => 1
[findQueryType] =>
[recursive] => 1
[order] =>
[virtualFields] => Array
(
)
[__associationKeys] => Array
(
[belongsTo] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => counterCache
)
[hasOne] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => dependent
)
[hasMany] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => limit
[6] => offset
[7] => dependent
[8] => exclusive
[9] => finderQuery
[10] => counterQuery
)
[hasAndBelongsToMany] => Array
(
[0] => className
[1] => joinTable
[2] => with
[3] => foreignKey
[4] => associationForeignKey
[5] => conditions
[6] => fields
[7] => order
[8] => limit
[9] => offset
[10] => unique
[11] => finderQuery
[12] => deleteQuery
[13] => insertQuery
)
)
[__associations] => Array
(
[0] => belongsTo
[1] => hasOne
[2] => hasMany
[3] => hasAndBelongsToMany
)
[__backAssociation] => Array
(
)
[__insertID] =>
[__numRows] =>
[__affectedRows] =>
[_findMethods] => Array
(
[all] => 1
[first] => 1
[count] => 1
[neighbors] => 1
[list] => 1
[threaded] => 1
)
[User] => User Object
(
[name] => User
[validate] => Array
(
[name] => Array
(
[rule] => Array
(
[0] => minLength
[1] => 4
)
[message] => Name has to be at least four characters
)
[email] => Array
(
[rule] => Array
(
[0] => email
)
[message] => Please enter a valid email
)
[username] => Array
(
[Username has to be at least four characters] => Array
(
[rule] => Array
(
[0] => minLength
[1] => 4
)
)
[This username is already taken, please try another] => Array
(
[rule] => isUnique
)
)
[password] => Array
(
[Password cannot be empty] => Array
(
[rule] => Array
(
[0] => notEmpty
)
)
[Password must be at least four characters] => Array
(
[rule] => Array
(
[0] => minLength
[1] => 4
)
)
[Passwords must match] => Array
(
[rule] => Array
(
[0] => passwordCompare
[1] => password_confirm
)
)
)
)
[useDbConfig] => default
[useTable] => users
[displayField] => id
[id] =>
[data] => Array
(
)
[table] => users
[primaryKey] => id
[_schema] => Array
(
[id] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
[key] => primary
)
[type] => Array
(
[type] => boolean
[null] =>
[default] => 1
[length] => 1
)
[is_active] => Array
(
[type] => boolean
[null] =>
[default] => 1
[length] => 1
)
[gender] => Array
(
[type] => boolean
[null] =>
[default] => 1
[length] => 1
)
[first_name] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[last_name] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[email] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[username] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[password] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[created] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[modified] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[bio] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 2048
[collate] => latin1_swedish_ci
[charset] => latin1
)
)
[validationErrors] => Array
(
)
[tablePrefix] =>
[alias] => User
[tableToModel] => Array
(
[users] => User
)
[logTransactions] =>
[cacheQueries] =>
[belongsTo] => Array
(
)
[hasOne] => Array
(
)
[hasMany] => Array
(
)
[hasAndBelongsToMany] => Array
(
)
[actsAs] =>
[Behaviors] => BehaviorCollection Object
(
[modelName] => User
[_attached] => Array
(
)
[_disabled] => Array
(
)
[__methods] => Array
(
)
[__mappedMethods] => Array
(
)
)
[whitelist] => Array
(
)
[cacheSources] => 1
[findQueryType] =>
[recursive] => 1
[order] =>
[virtualFields] => Array
(
)
[__associationKeys] => Array
(
[belongsTo] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => counterCache
)
[hasOne] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => dependent
)
[hasMany] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => limit
[6] => offset
[7] => dependent
[8] => exclusive
[9] => finderQuery
[10] => counterQuery
)
[hasAndBelongsToMany] => Array
(
[0] => className
[1] => joinTable
[2] => with
[3] => foreignKey
[4] => associationForeignKey
[5] => conditions
[6] => fields
[7] => order
[8] => limit
[9] => offset
[10] => unique
[11] => finderQuery
[12] => deleteQuery
[13] => insertQuery
)
)
[__associations] => Array
(
[0] => belongsTo
[1] => hasOne
[2] => hasMany
[3] => hasAndBelongsToMany
)
[__backAssociation] => Array
(
)
[__insertID] =>
[__numRows] =>
[__affectedRows] =>
[_findMethods] => Array
(
[all] => 1
[first] => 1
[count] => 1
[neighbors] => 1
[list] => 1
[threaded] => 1
)
)
[Child] => Post Object
*RECURSION*
)
Below is a posts model, and as you can see I have a belongsTo relationship on each post, so that the user details are also retrieved with the post.
I would also like the exact same behaviour for the Posts.Children (see the hasMany definition below).
QUESTION:
How do I also apply the belongsTo relationship to the Posts.Children ( Posts.Child belongsTo User )??
<?php
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'post_owner',
'fields' => array( "User.id" , "User.first_name" , "User.last_name" )
)
);
// -=>
var $hasMany = array(
'Child' => array(
'className' => 'Post',
'foreignKey' => 'parent_id',
'conditions' => array( "Child.active" => "1" ),
'limit' => '2',
'order' => 'time DESC'
)
);
}
?>
And here is the PostsController Code which I am currently using:
class PostsController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Posts';
function index() {
$conditions = array( "Post.parent_id" => "0" , "Post.active" => "1" );
$this->set( 'posts', $this->Post->find('all',array( 'conditions'=>$conditions )) );
debug( $this->Post->find('all',array( 'conditions'=>$conditions )) );
}
"what's the output if you call debug($this->Post->Child); in the posts controller?"
Here it is:
Post Object
(
[name] => Post
[hasMany] => Array
(
[Child] => Array
(
[className] => Post
[foreignKey] => parent_id
[conditions] => Array
(
[Child.active] => 1
)
[fields] =>
[order] =>
[limit] =>
[offset] =>
[dependent] =>
[exclusive] =>
[finderQuery] =>
[counterQuery] =>
)
)
[belongsTo] => Array
(
[User] => Array
(
[className] => User
[foreignKey] => dom
[fields] => Array
(
[0] => User.id
[1] => User.first_name
[2] => User.last_name
)
[conditions] =>
[order] =>
[counterCache] =>
)
)
[useDbConfig] => default
[useTable] => posts
[displayField] => title
[id] =>
[data] => Array
(
)
[table] => posts
[primaryKey] => id
[_schema] => Array
(
[active] => Array
(
[type] => integer
[null] =>
[default] => 1
[length] => 1
)
[id] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
[key] => primary
)
[parent_id] => Array
(
[type] => integer
[null] =>
[default] => 0
[length] => 11
)
[time] => Array
(
[type] => integer
[null] =>
[default] => 0
[length] => 11
)
[dom] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
)
[sub] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
)
[created] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[modified] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[text] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 4096
[collate] => latin1_swedish_ci
[charset] => latin1
)
)
[validate] => Array
(
)
[validationErrors] => Array
(
)
[tablePrefix] =>
[alias] => Child
[tableToModel] => Array
(
[posts] => Child
[users] => User
)
[logTransactions] =>
[cacheQueries] =>
[hasOne] => Array
(
)
[hasAndBelongsToMany] => Array
(
)
[actsAs] =>
[Behaviors] => BehaviorCollection Object
(
[modelName] => Child
[_attached] => Array
(
)
[_disabled] => Array
(
)
[__methods] => Array
(
)
[__mappedMethods] => Array
(
)
)
[whitelist] => Array
(
)
[cacheSources] => 1
[findQueryType] =>
[recursive] => 1
[order] =>
[virtualFields] => Array
(
)
[__associationKeys] => Array
(
[belongsTo] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => counterCache
)
[hasOne] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => dependent
)
[hasMany] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => limit
[6] => offset
[7] => dependent
[8] => exclusive
[9] => finderQuery
[10] => counterQuery
)
[hasAndBelongsToMany] => Array
(
[0] => className
[1] => joinTable
[2] => with
[3] => foreignKey
[4] => associationForeignKey
[5] => conditions
[6] => fields
[7] => order
[8] => limit
[9] => offset
[10] => unique
[11] => finderQuery
[12] => deleteQuery
[13] => insertQuery
)
)
[__associations] => Array
(
[0] => belongsTo
[1] => hasOne
[2] => hasMany
[3] => hasAndBelongsToMany
)
[__backAssociation] => Array
(
)
[__insertID] =>
[__numRows] =>
[__affectedRows] =>
[_findMethods] => Array
(
[all] => 1
[first] => 1
[count] => 1
[neighbors] => 1
[list] => 1
[threaded] => 1
)
[User] => User Object
(
[name] => User
[validate] => Array
(
[name] => Array
(
[rule] => Array
(
[0] => minLength
[1] => 4
)
[message] => Name has to be at least four characters
)
[email] => Array
(
[rule] => Array
(
[0] => email
)
[message] => Please enter a valid email
)
[username] => Array
(
[Username has to be at least four characters] => Array
(
[rule] => Array
(
[0] => minLength
[1] => 4
)
)
[This username is already taken, please try another] => Array
(
[rule] => isUnique
)
)
[password] => Array
(
[Password cannot be empty] => Array
(
[rule] => Array
(
[0] => notEmpty
)
)
[Password must be at least four characters] => Array
(
[rule] => Array
(
[0] => minLength
[1] => 4
)
)
[Passwords must match] => Array
(
[rule] => Array
(
[0] => passwordCompare
[1] => password_confirm
)
)
)
)
[useDbConfig] => default
[useTable] => users
[displayField] => id
[id] =>
[data] => Array
(
)
[table] => users
[primaryKey] => id
[_schema] => Array
(
[id] => Array
(
[type] => integer
[null] =>
[default] =>
[length] => 11
[key] => primary
)
[type] => Array
(
[type] => boolean
[null] =>
[default] => 1
[length] => 1
)
[is_active] => Array
(
[type] => boolean
[null] =>
[default] => 1
[length] => 1
)
[gender] => Array
(
[type] => boolean
[null] =>
[default] => 1
[length] => 1
)
[first_name] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[last_name] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[email] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[username] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[password] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 200
[collate] => latin1_swedish_ci
[charset] => latin1
)
[created] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[modified] => Array
(
[type] => datetime
[null] =>
[default] =>
[length] =>
)
[bio] => Array
(
[type] => string
[null] =>
[default] =>
[length] => 2048
[collate] => latin1_swedish_ci
[charset] => latin1
)
)
[validationErrors] => Array
(
)
[tablePrefix] =>
[alias] => User
[tableToModel] => Array
(
[users] => User
)
[logTransactions] =>
[cacheQueries] =>
[belongsTo] => Array
(
)
[hasOne] => Array
(
)
[hasMany] => Array
(
)
[hasAndBelongsToMany] => Array
(
)
[actsAs] =>
[Behaviors] => BehaviorCollection Object
(
[modelName] => User
[_attached] => Array
(
)
[_disabled] => Array
(
)
[__methods] => Array
(
)
[__mappedMethods] => Array
(
)
)
[whitelist] => Array
(
)
[cacheSources] => 1
[findQueryType] =>
[recursive] => 1
[order] =>
[virtualFields] => Array
(
)
[__associationKeys] => Array
(
[belongsTo] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => counterCache
)
[hasOne] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => dependent
)
[hasMany] => Array
(
[0] => className
[1] => foreignKey
[2] => conditions
[3] => fields
[4] => order
[5] => limit
[6] => offset
[7] => dependent
[8] => exclusive
[9] => finderQuery
[10] => counterQuery
)
[hasAndBelongsToMany] => Array
(
[0] => className
[1] => joinTable
[2] => with
[3] => foreignKey
[4] => associationForeignKey
[5] => conditions
[6] => fields
[7] => order
[8] => limit
[9] => offset
[10] => unique
[11] => finderQuery
[12] => deleteQuery
[13] => insertQuery
)
)
[__associations] => Array
(
[0] => belongsTo
[1] => hasOne
[2] => hasMany
[3] => hasAndBelongsToMany
)
[__backAssociation] => Array
(
)
[__insertID] =>
[__numRows] =>
[__affectedRows] =>
[_findMethods] => Array
(
[all] => 1
[first] => 1
[count] => 1
[neighbors] => 1
[list] => 1
[threaded] => 1
)
)
[Child] => Post Object
*RECURSION*
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将其放入
Children
模型中。顺便说一句,您发布的是模型,而不是控制器。希望这只是一个拼写错误,并且您将其放在正确的位置。
还不清楚您将该模型称为“Child”还是“Children”。
$hasMany
关系使用Children
,但在您说Posts.Child
的问题中 - 您是否正确设置了词形变化?编辑:我可能误解了这个问题:如果你想要一个 hasMany 关系,那么你已经拥有了。问题要么出在检索数据的方式上,要么出在关系定义上。
You put it in the
Children
model.By the way, what you posted is the model, not the controller. Hopefully it's just a typo and you have that in the correct place.
It's also unclear if you call the model "Child" or "Children". The
$hasMany
relationship is usesChildren
, but in the question you sayPosts.Child
-- do you have the inflection set up correctly?EDIT: I may have misunderstood the question: if you want a hasMany relationship, you already have that. The problem is either in the way you retrieve the data or in the relationship definition.
你不需要做任何事情,一个 Child 也是一个 Post,所以也属于一个 User。 (你应该将其命名为“Child”而不是“Children”)
对于可包含的,请尝试以下操作:
$this->Post->find('all',array('contain'=>array('Child) '=>array('用户'))));
you don't have to do anything, a Child is also a Post, so also belongsTo a User. (And you should name it "Child" not "Children")
With containable, try this:
$this->Post->find('all',array('contain'=>array('Child'=>array('User'))));