Heim >Backend-Entwicklung >PHP-Tutorial >Thinkphp 使用join()方法以后无法在使用field()方法
<code> // 获取所有的支出记录 public function gainHavingPayRecord(){ $m = M('pay_record'); $result = $m->order('payrecord_pay_time desc') ->join("LEFT JOIN x_account ON x_pay_record.payrecord_payer = x_account.account_id") ->field('account_phone,account_password,account_salt,account_alipay,account_createtime,account_cash,account_bank,account_isdelete',true) ->select(); dump($result[0]); return $result; }</code>
之后输出的数据所有account表中的数据都被过滤了 只输出了pay_record表中的数据
<code>array(10) { ["payrecord_id"] => string(1) "1" ["payrecord_pay_time"] => string(1) "0" ["payrecord_payer"] => string(1) "1" ["payrecord_payment_type"] => string(1) "1" ["payrecord_item_category"] => string(1) "1" ["payrecord_item_son_category"] => string(1) "1" ["payrecord_sum"] => string(4) "3000" ["payrecord_record_time"] => string(10) "1467428218" ["payrecord_confirm"] => string(1) "0" ["payrecord_confirm_time"] => string(1) "0" }</code>
官方文档是这么说的
请问还有有什么方法可以吧这些字段过滤掉吗
<code>->field('account_phone,account_password,account_salt,account_alipay,account_createtime,account_cash,account_bank,account_isdelete',true)</code>
其实我也可以直接把想要使用的数据存到一张表里就可以不用join()方法减少服务器压力.但是遇到这种问题我想解决了 ,一旦那天非常迫切的需要这样处理数据也不至于无从下手.
<code> // 获取所有的支出记录 public function gainHavingPayRecord(){ $m = M('pay_record'); $result = $m->order('payrecord_pay_time desc') ->join("LEFT JOIN x_account ON x_pay_record.payrecord_payer = x_account.account_id") ->field('account_phone,account_password,account_salt,account_alipay,account_createtime,account_cash,account_bank,account_isdelete',true) ->select(); dump($result[0]); return $result; }</code>
之后输出的数据所有account表中的数据都被过滤了 只输出了pay_record表中的数据
<code>array(10) { ["payrecord_id"] => string(1) "1" ["payrecord_pay_time"] => string(1) "0" ["payrecord_payer"] => string(1) "1" ["payrecord_payment_type"] => string(1) "1" ["payrecord_item_category"] => string(1) "1" ["payrecord_item_son_category"] => string(1) "1" ["payrecord_sum"] => string(4) "3000" ["payrecord_record_time"] => string(10) "1467428218" ["payrecord_confirm"] => string(1) "0" ["payrecord_confirm_time"] => string(1) "0" }</code>
官方文档是这么说的
请问还有有什么方法可以吧这些字段过滤掉吗
<code>->field('account_phone,account_password,account_salt,account_alipay,account_createtime,account_cash,account_bank,account_isdelete',true)</code>
其实我也可以直接把想要使用的数据存到一张表里就可以不用join()方法减少服务器压力.但是遇到这种问题我想解决了 ,一旦那天非常迫切的需要这样处理数据也不至于无从下手.
<code>字段前加上表名或者表的别名: $result = M('x_account as acc')->join('x_pay_record as rec on rec .payrecord_payer = acc.account_id')->field('acc.字段1, acc.字段2, rec.字段3, rec.字段4')->select(); </code>
join后必须要使用表名的 因为mysql不知道你要哪张表的哪个字段过滤那个表的哪个字段
应该有.吧,比如说pay_record.acc
你可以先用getLastSql看下sql语句是怎么写的,join和field应该是可以结合的,所以你再检查一下你的关键字。
加一句test
field里面要加上表名field('表名.account_phone,表名.account_password,表名.account_salt,表名.account_alipay,表名.account_createtime,表名.account_cash,表名.account_bank,表名.account_isdelete',true)