search
Homephp教程php手册3.2.2模型关联condition条件使用变量方法

先前使用关联模型的时候出现增加没有问题,但是 删除和修改 的时候出现了不同步的原因。

大家可以加QQ群 161607701 ,大家共同学习PHP技术!

说明:
$_link 内如果设置了条件的话就会出现以上问题。
我是只配置了 一对一 其他我没有测试哦。因为我只用到了一对一表。
解决办法如下
打开:TP/Library/Think/Model/RelationModel.class.php 文件
查找 一下代码:                        if(!empty($val['condition'])) {<br>                             $mappingCondition   =   $val['condition'];<br>                         }else{<br>                             $mappingCondition               =   array();<br>                             $mappingCondition[$mappingFk]   =   $pk;<br>                         }修改为:                        $mappingCondition               =   array();<br>                         if(!empty($val['condition'])) {<br>                             if(!empty($val['is_key'])){<br>                                 $mappingCondition   =   $this->get_condition($val['condition']);<br>                             }else{<br>                                 $mappingCondition = $this->get_condition($val['condition']);<br>                                 $mappingCondition[$mappingFk] =  $pk;<br>                             }<br>                         }else{<br>                             $mappingCondition[$mappingFk]   =   $pk;<br>                         }在最后加入一个方法    /**<br>      * @param $condition 条件<br>      * @return array<br>      */<br>     protected function get_condition($condition){<br>         $wherearr =  explode('and',$condition);<br>         $mappingCondition = array();<br>         foreach($wherearr as $value){<br>             $value  =   trim($value);<br>             list($key,$v) = explode('=',$value);<br>             $key = trim($key);<br>             $v =  str_replace('"','',str_replace('\'','',trim($v)));<br>             $mappingCondition[$key] =  $v;<br>         }<br>         return $mappingCondition;<br>     }由于增加了一个参数 is_key
需要 配置一下。
'is_key' => false在配置关联表的时候 condition 会自动带着 主表ID 作为条件。
'is_key' => true, 和 thinkPhp 默认的一样了,就会是你 配置的 condition 参数内作为条件。 //数据关联<br>     protected $_link=array(<br>         'Keyword'=>array(<br>             'mapping_type'    => self::HAS_ONE,<br>             'class_name'    =>'Common/Keyword',<br>             'foreign_key'   => 'mod_id',<br>             'is_key' => false,<br>             'mapping_fields'=> 'keyword,mod_name,match_type',<br>             'condition' => 'mod_name = "TextReply"',<br>             'as_fields' => 'keyword,mod_name,match_type',<br>         )<br>     );

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),