cakephp mysql,Cakephp 2.0 MySQL查询

I'm new in Cakephp 2.0, but I want to make a view of two tables. I have the following tables:

hpsas with records: ciname, location, status

ldaps with records: ciname, status

In my Controller I have the following syntax:

$this->Hpsa->query("SELECT `hpsas`.`ciname`, `hpsas`.`status`, `ldaps`.`ciname`, `ldaps`.`status` FROM `cmdb`.`hpsas`, `cmdb`.`ldaps` WHERE `hpsas`.`ciname` = `ldaps`.`ciname`;");

I got the following results as expected:

'hpsas' => array(

(int) 0 => array(

'hpsas' => array(

'ciname' => 'lsrv8001',

'status' => 'live'

),

'ldaps' => array(

'ciname' => 'lsrv8001',

'status' => 'indeployment''

How do I make the Model and Controller with the right Cakephp 2.0 syntax?

解决方案

Not sure what you already done and how your controller/model is named so I just put code samples which may help understand basic idea.

In Hpsas model "many to one" relationship is defined.

class Hpsas extends AppModel {

public $belongsTo = array(

'uniqueAlias1' => array(

'className' => 'Ldaps',

'foreignKey' => 'ciname'

)

);

/...

In Ldaps model "one to many" relationship is defined.

class Ldaps extends AppModel {

public $hasMany = array(

'uniqueAlias2' => array(

'className' => 'Hpsas',

'foreignKey' => 'ciname'

),

);

/...

Now if perform code $this->Hpsas->find('all') on Hpsass controller you will likely get following results:

array(

(int) 0 => array(

'uniqueAlias1' => array(

//hpsas table row with value

),

'uniqueAlias2' => array(

//ldaps table row where hpsas.ciname = ldaps.ciname

)

),

//rest hpsas table rows

)

Don't get confused about alias I used in samples, you could named whatever you want. They come handy when need dealing with multiples association for same model.

More detailed explained samples could be found on documentation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值