findall(),有这种情况的条件怎么写
发布于:2022-01-17 09:50:54
#1 253618
示例中有这么一段
$conditions = array("user_name"=>"小羊羊");这个是主表中的条件,(如果是从表中的呢?)
$userObj = spClass("user");
$result = $userObj->spLinker()->find($conditions);
dump($result);
如果那个条件是从表中条件要怎么写
2010-08-26 16:06:54
#2 okbb
user_id int 用户ID 自增/主键
user_name varchar(50) 用户名 唯一
user_detail 用户详细资料表
user_id int 用户ID 主键
intro varchar(255) 用户个人介绍
从表中已经有了user_id,splinker()会自动,不要写了
2010-08-26 16:33:50
#3 253618
$result = $userObj->spLinker()->find($conditions);
$conditions好像只能写主表的条件,如果要写从表的要怎么写。
我说的只是一个例子,也许你们可能不会碰到,可是我刚好碰到了,最后没办法,只好主从表掉换一下
2010-08-26 20:36:29
#4 okbb
一样写,
表一:userid username
表二:userid userage
你说哪个是主表,哪个就是。分不清的。。。
2010-08-26 20:39:14
#5 jake
spLinker的参数'condition'中可以做从表的条件,作为对关联结果的限制
http://speedphp.com/post/splinker-hasone.html var $linker = array(
array(
'type' => 'hasmany',
'map' => 'ulink',
'mapkey' => 'user_id',
'fclass' => 'gb',
'fkey' => 'user_id',
'midclass' => 'user2gb',
'sort' => '',
'condition' => '', 'field' => '', 'limit' => '',
'enabled' => true, 'countonly' => false
),
array(
...
)
);
这个参数和findAll的第一个参数$condition是一样使用的方法。
2010-08-26 20:57:52
#6 253618
能不能给个具体的,最好能来个从表条件的完整$condition的写法,主表直接字段名就行,从表试了好多种都不行
2010-08-31 22:23:59
#7 jake
var $linker = array(
array(
'type' => 'hasmany',
'map' => 'my_detail',
'mapkey' => 'user_id',
'fclass' => 'user_detail',
'fkey' => 'user_id',
'enabled' => true,
),
);
上面是user主表,user_detail从表的普通写法( $linker在user模型里面,这个是spLinker的基础用法)
然后比如说从表内有个字段叫gerenal(性别)的,然后你关联只想查出男的(gerenal=1)
var $linker = array(
array(
'type' => 'hasmany',
'map' => 'my_detail',
'mapkey' => 'user_id',
'fclass' => 'user_detail',
'fkey' => 'user_id',
'condition' => array('gerenal'=>1), // 当然还可以加其他条件,或者直接使用字符串作为条件'gerenal=1' 'enabled' => true,
),
);
如果你想从表按user_id排序的
var $linker = array(
array(
'type' => 'hasmany',
'map' => 'my_detail',
'mapkey' => 'user_id',
'fclass' => 'user_detail',
'fkey' => 'user_id',
'condition' => array('gerenal'=>1),
'sort' => 'user_id DESC', 'enabled' => true,
),
);
如果你想从表只取intro的
var $linker = array(
array(
'type' => 'hasmany',
'map' => 'my_detail',
'mapkey' => 'user_id',
'fclass' => 'user_detail',
'fkey' => 'user_id',
'condition' => array('gerenal'=>1),
'sort' => 'user_id DESC',
'field' => 'intro,user_id', // 当然,主键user_id还是得加上的
'enabled' => true,
),
);
如果你想从表只取10条记录的
var $linker = array(
array(
'type' => 'hasmany',
'map' => 'my_detail',
'mapkey' => 'user_id',
'fclass' => 'user_detail',
'fkey' => 'user_id',
'condition' => array('gerenal'=>1),
'sort' => 'user_id DESC',
'field' => 'intro,user_id', // 当然,主键user_id还是得加上的
'limit' => 10, 'enabled' => true,
),
);
2010-08-31 22:39:31
#8 253618
这样是成功了,谢谢,原来只能是在model里写呀,可是在controller里要怎么写呢
2010-09-01 16:54:53
#9 jake
在controller里面可以通过
$userObj->linker[0]['limit'] = 10;
$userObj->linker[0]['condition'] = array(xxx);
$userObj->spLinker()->findAll();
这种方式来写。
不过是不建议在controller里面写,因为数据关联是M层的东西,最好在M层就定义好了。
2010-09-01 16:59:19
#10 253618
非常感谢,刚好有好几种情况要查询从表,如果都写在M层,那不是要建好几个model
2010-09-01 22:08:58
#11 253618
我试了你上面那方法,查询条件无效
2010-09-01 22:26:40
#12 jake
我试了你上面那方法,查询条件无效
253618 发表于 2010-9-1 22:26
具体说说看哪个无效?
关联条件仅是一个数组,按照普通的数组操作方式去做就可以了。
2010-09-02 08:40:18
#13 253618
回复 9 jake 这样的写法,条件不起作用
2010-09-02 10:58:48
#14 jake
回复 jake
这样的写法,条件不起作用
253618 发表于 2010-9-2 10:58
嗯,关联限制$condition的确是这样设置的,你可以检查一下代码,输出$linker看看。
另外,我上面说到的性别的字段,只是举例说明而已,你可以针对你自己的需要,使用其他字段进行对关联结果的限制。
2010-09-02 13:35:25