CakePHP - 关系问题 - Posts.Child 属于用户,如何定义?
Posted
技术标签:
【中文标题】CakePHP - 关系问题 - Posts.Child 属于用户,如何定义?【英文标题】:CakePHP - Relationship Question - Posts.Child belongsTo User, How to define? 【发布时间】:2011-08-08 13:52:04 【问题描述】:下面是一个帖子模型,如您所见,我在每个帖子上都有一个 belongsTo 关系,因此用户详细信息也与帖子一起检索。 我也希望 Posts.Children 具有完全相同的行为(请参阅下面的 hasMany 定义)。
问题:
我如何也将 belongsTo 关系应用到 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'
)
);
?>
这是我目前使用的 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 )) );
“如果你在 post 控制器中调用 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*
)
【问题讨论】:
我仍然在这里寻找建议。 -> 如何让每个帖子和每个帖子的每个子项显示用户信息?每个帖子和每个孩子都有一个“所有者”字段,我需要在视图中的该元素上显示每个帖子/孩子的名字和姓氏。谢谢 【参考方案1】:你把它放在Children
模型中。
var $belongsTo = array( 'Post' );
顺便说一句,您发布的是模型,而不是控制器。希望这只是一个错字,并且您将其放在正确的位置。
还不清楚您将模型称为“儿童”还是“儿童”。 $hasMany
关系是使用Children
,但在你说Posts.Child
的问题中——你是否正确设置了变形?
编辑:我可能误解了这个问题:如果你想要一个 hasMany 关系,你已经有了。问题在于您检索数据的方式或关系定义。
【讨论】:
以上是帖子模型。我没有其他与帖子相关的模型,我应该为孩子们提供另一个单独的模型吗?? 如果你想拥有 Children belongsTo Post 关系,那么是的,你需要 Children 模型。 (你的意思是“不是任何其他与帖子相关的模型”?你有用户模型相关,对吧?) 我目前有一个用户模型和一个帖子模型。 Post Model 就在上面了,目前在 User Model 中还没有提到 Post Model。我只想能够从“app/views/posts/index.ctp 中访问 User.first_name 和 User.last_name【参考方案2】:你不必做任何事情,Child 也是 Post,所以也属于 User。 (你应该把它命名为“孩子”而不是“孩子”)
使用containable,试试这个:$this->Post->find('all',array('contain'=>array('Child'=>array('User'))));
【讨论】:
好的,谢谢。但是 DEBUG() 仍然只显示帖子的用户数据,而不是每个帖子的孩子。??对此感到抱歉;) Child 似乎不被视为 Post。感谢您的帮助;) 我看到您的 Child 对象中有一个 User 对象。只需确保您已启用可包含且数据库中有一些正确的数据。以上是关于CakePHP - 关系问题 - Posts.Child 属于用户,如何定义?的主要内容,如果未能解决你的问题,请参考以下文章
CakePHP:表单输入(呈现为选择)未正确处理 hasOne 关系