论坛首页 编程语言技术论坛

cakephp里一对多关系的查询的问题

浏览 2849 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-12   最后修改:2009-03-29
PHP
User model的代码:
<?php
	class User extends AppModel {
		var $name = 'User';

		var $hasOne = array(
			'Spreadcode' => array(
				'className' => 'Spreadcode',
				'dependent' => true,
				'foreignKey' => 'uid'
			),
			'Spreadpage' => array(
				'className' => 'Spreadpage',
				'dependent' => true,
				'foreignKey' => 'uid'
			),
			'Infospreader' => array(
				'className' => 'Infospreader',
				'dependent' => true,
				'foreignKey' => 'uid'
			)
		);

		var $hasMany = array(
			'Leavewordofcustomer' => array(
				'className' => 'Leavewordofcustomer',
				'dependent' => true,
				'foreignKey' => 'uid'
			)
		);
	}
?>
leavewordofcustomer的model:
<?php
	class Leavewordofcustomer extends AppModel {
		var $name = 'Leavewordofcustomer';

		var $belongsTo = array(
			'User' => array(
				'className' => 'User',
				'dependent' => true,
				'foreignKey' => 'uid'
			)
		);
	}
?>
我在user的controller里有一段查询,根据Spreadcode.code字段查询Spreadcode.uid,根据这个uid得到User.id,再根据User.id查询leavewordofcustomer表里的记录(每个用户id在leavewordofcustomer表里有多条记录)
$params = array(
	'conditions' => array('Spreadcode.code' => $sc),
	'fields' => array('Leavewordofcustomer.leaveWord'),
);
$rs = $this->User->find('all', $params);
在debug模式下看输出的sql语句是:
SELECT `Leavewordofcustomer`.`leaveWord`, `User`.`id` FROM `users` AS `User` LEFT JOIN `spreadcodes` AS `Spreadcode` ON (`Spreadcode`.`uid` = `User`.`id`) LEFT JOIN `spreadpages` AS `Spreadpage` ON (`Spreadpage`.`uid` = `User`.`id`) LEFT JOIN `infospreaders` AS `Infospreader` ON (`Infospreader`.`uid` = `User`.`id`) WHERE `Spreadcode`.`code` = '003200' LIMIT 5
系统提示:
1054: Unknown column 'Leavewordofcustomer.leaveWord' in 'field list'
不知道这种稍微复杂一些的一对多关系的查询应该怎么处理?
   发表时间:2009-04-07   最后修改:2009-04-07
我知道两个方法
1.分两次find 先查出User.uid 再去$this->User->Leavewordofcustomer->find中查。

2.设置recursive为1,find不要加fields条件,用pr($rs)显示出结果集看看 应该有Leavewordofcustomer的数据了。

你可以注意下recursive这个属性的效果。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics